home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / clessons.arc / CLESSONS.TXT
Text File  |  1986-02-15  |  66KB  |  6,017 lines

  1. This disk contains demostration programs that parallel the Kernighan and
  2. Ritchie book: The C Programming Language.  The programs are grouped by
  3. chapter (C-CH.1 thru C-CH.7). Each chapter contains various examples or
  4. C programs that demostrate a function or method.  These programs are not
  5. meant to be finished C programs, rather they were created for classroom
  6. use as an aide.  Most of these programs are in general C libraries although
  7. generally in a greatly enhanced form.
  8.  
  9. One warning.
  10. These programs were created on a UNIX system, an AT&T 3B20s and a
  11. Digital VAX 11/70.  Keep this in mind while using the files.
  12.  
  13. The file 'C-FUNCT', is a collection of more usable function calls that
  14. may be used and called within a C program.
  15.  
  16. The file 'C-PRG.LS', is the complete listing of programs included on this
  17. disk.  For information, the listing is the output from the UNIX version
  18. of the DOS 'dir' command.  The first column, mostly -rw-r--r-- is the file
  19. permissions.  The first (-) is the directory place, the letter 'd', denotes
  20. that this entry is a directory, the hyphen, denotes that the entry is a file.
  21. The next 3 positions (rw-), show the permissions for the owner of the file.
  22. Read and Write permissions are in force here.  The hyphen in the 3rd position
  23. denotes that the 'execute' permission is not in force.  In the last 6
  24. positions, the first 3 are for the group permissions and the last 3 are for
  25. the public permissions.  In this example, the group and public have only
  26. read permissions.  The next three columns, mostly (1 cp18     cp), indicate
  27. quanity of links and owners (cp18 is the login).  The rest is roughly
  28. the same as 'dir'.
  29.  
  30.  
  31. pr 1.*
  32.  
  33.  
  34. Jul 31 13:15 1984  1.00array.c Page 1
  35.  
  36.  
  37. /* example of arrays */
  38.  
  39. #define EOF -1
  40. #define SZ  25
  41. main()  /* count digits 0 - 9 and store in elements 0 - 9 */
  42. {
  43.         int c, i, digit[SZ];            /* declaration */
  44.         while (i = 1; i < SZ; ++i)      /* initialization */
  45.               digit[i - 1] = i;
  46.         printf("digits =");
  47.         for (i = 0; i < SZ; ++i)
  48.                 printf("%4d", digit[i]);
  49.         printf("\n");
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. Jul 27 17:12 1984  1.10first.c Page 1
  101.  
  102.  
  103. /* This is a C program that prints a
  104. /* message to standard output
  105. /* (usually your terminal) */
  106.  
  107. main()
  108. {
  109.         printf("This is a C program.\n");
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166. Jul 27 17:12 1984  1.29while.c Page 1
  167.  
  168.  
  169. /* print the numbers 1 - 10 with squares and cubes */
  170.  
  171. main()
  172. {
  173.         int     st, end, step, num, sq, cube;
  174.         st = 1;
  175.         end = 10;
  176.         step = 1;
  177.         num = st;
  178.         while (num <= end)
  179.         {       sq = num * num;
  180.                 cube = num * sq;
  181.                 printf("%6d %6d %6d\n", num, sq, cube);
  182.                 num = num + step;
  183.         }
  184. }
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232. Jul 27 17:12 1984  1.49for.c Page 1
  233.  
  234.  
  235. /* also prints the numbers 1 - 10 with squares
  236.    and cubes - this time with a "for" loop */
  237.  
  238. main()
  239. {
  240.         int n;
  241.  
  242.         for (n = 1; n <= 10; n = n + 1)
  243.                 printf("%6d %6d %6d\n", n, n * n, n * n * n);
  244. }
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298. Jul 27 17:12 1984  1.53inc.c Page 1
  299.  
  300.  
  301. /* introduces the increment operator */
  302.  
  303. main()
  304. {
  305.         int n;
  306.  
  307.         for (n = 1; n <= 10; ++n)
  308.                 printf("%6d %6d %6d\n", n, n * n, n * n * n);
  309. }
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364. Jul 27 17:12 1984  1.55symbolic.c Page 1
  365.  
  366.  
  367. /* introduces symbolic constants */
  368.  
  369. #define MAX     10
  370. #define SQ      n * n
  371. #define CU      n * n * n
  372. main()
  373. {
  374.         int n;
  375.  
  376.         for (n = 1; n <= MAX; ++n)
  377.                 printf("%6d %6d %6d\n", n, SQ, CU);
  378. }
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430. Jul 27 17:12 1984  1.58copy.c Page 1
  431.  
  432.  
  433. /* copy input to output */
  434.  
  435. #define EOF -1
  436.  
  437. main()
  438. {
  439.         int c;
  440.         c = getchar();          /* see getc(3) */
  441.         while (c != EOF) {
  442.           putchar(c);           /* see putc(3) */
  443.           c = getchar();
  444.         }
  445. }
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496. Jul 27 17:12 1984  1.62copy2.c Page 1
  497.  
  498.  
  499. /* assignments within tests */
  500.  
  501. #define EOF -1
  502. main()  /* copy input to output; 2nd version */
  503. {
  504.         int c;
  505.  
  506.         while ((c = getchar()) != EOF)
  507.                 putchar(c);
  508. }
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562. Jul 27 17:12 1984  1.64ccount.c Page 1
  563.  
  564.  
  565. /* character counting program */
  566.  
  567. #define EOF -1
  568. main()  /* count number of characters received */
  569. {
  570.         double  nc;
  571.  
  572.         for (nc = 0; getchar() != EOF; ++nc)
  573.                         ;       /* do nothing */
  574.         printf("char count is %.0f\n", nc);
  575. }
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601.  
  602.  
  603.  
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628. Jul 27 17:12 1984  1.67lcount.c Page 1
  629.  
  630.  
  631. /* count lines */
  632.  
  633. #define EOF -1
  634. main()  /* count number of lines in input */
  635. {
  636.         int c;
  637.         long nl = 0; /* combined declaration and initialization */
  638.  
  639.         while ((c = getchar()) != EOF)
  640.                 if (c == '\n')
  641.                         ++nl;
  642.         printf("line count is %ld\n", nl);
  643. }
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.  
  667.  
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694. Jul 27 17:12 1984  1.71uccount.c Page 1
  695.  
  696.  
  697. /* count upper case and others */
  698.  
  699. #define EOF -1
  700. main()  /* print count of upper case letters
  701.         /* & all other characters received */
  702. {
  703.         int c, uc, oth;
  704.  
  705.         uc = oth = 0;   /* multiple assignment */
  706.         while ((c = getchar()) != EOF)
  707.                 if      (c >= 'A' && c <= 'Z')
  708.                         ++uc;
  709.                 else    ++oth;
  710.         printf("uc is %d, oth is %d\n", uc, oth);
  711. }
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732.  
  733.  
  734.  
  735.  
  736.  
  737.  
  738.  
  739.  
  740.  
  741.  
  742.  
  743.  
  744.  
  745.  
  746.  
  747.  
  748.  
  749.  
  750.  
  751.  
  752.  
  753.  
  754.  
  755.  
  756.  
  757.  
  758.  
  759.  
  760. Jul 27 17:12 1984  1.73lwccount.c Page 1
  761.  
  762.  
  763. /* line, word, and char count */
  764. #define EOF -1
  765. #define YES  1
  766. #define NO   0
  767. main()  /* count lines, words, chars in input */
  768. {       int c, nl, nw, inword = NO;
  769.         long nc;
  770.         nl = nw = nc = 0;
  771.         while ((c = getchar()) != EOF)
  772.         {       ++nc;
  773.                 if (c == '\n')
  774.                         ++nl;
  775.                 if      (c == ' ' || c == '\t' || c == '\n')
  776.                         inword = NO;
  777.                 else if (inword == NO)
  778.                 {       inword = YES;
  779.                         ++nw;
  780.                 }
  781.         }
  782.         printf("lines, words, chars: %d %d %ld\n", nl, nw, nc);
  783. }
  784.  
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.  
  797.  
  798.  
  799.  
  800.  
  801.  
  802.  
  803.  
  804.  
  805.  
  806.  
  807.  
  808.  
  809.  
  810.  
  811.  
  812.  
  813.  
  814.  
  815.  
  816.  
  817.  
  818.  
  819.  
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826. Jul 27 17:12 1984  1.77power.c Page 1
  827.  
  828.  
  829. /* example of function calls */
  830.  
  831. main()  /* test power() for range 1 - 9 */
  832. {
  833.         int i;
  834.         printf("   Num   Sq\n");
  835.         for (i = 1; i < 10; ++i)
  836.                 printf("%5d %5d\n", i, power(i, 2));
  837. }
  838. power(x, n)     /* raise x to the n-th power; n > 0 */
  839. int x, n;       /* declaration of arguments */
  840. {
  841.         int i, p = 1;
  842.         for (i = 1; i <= n; ++i)
  843.                 p = p * x;
  844.         return (p);
  845. }
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.  
  863.  
  864.  
  865.  
  866.  
  867.  
  868.  
  869.  
  870.  
  871.  
  872.  
  873.  
  874.  
  875.  
  876.  
  877.  
  878.  
  879.  
  880.  
  881.  
  882.  
  883.  
  884.  
  885.  
  886.  
  887.  
  888.  
  889.  
  890.  
  891.  
  892. Jul 27 17:12 1984  1.85array.c Page 1
  893.  
  894.  
  895. /* example of arrays */
  896.  
  897. #define EOF -1
  898. #define SZ  10
  899. main()  /* count digits 0 - 9 and store in elements 0 - 9 */
  900. {
  901.         int c, i, digit[SZ];            /* declaration */
  902.         for (i = 0; i < SZ; ++i)        /* initialization */
  903.                 digit[i] = 0;
  904.         while ((c = getchar()) != EOF)
  905.                 if (c >= '0' && c <= '9')
  906.                         ++digit[c - '0'];
  907.         printf("digits =");
  908.         for (i = 0; i < SZ; ++i)
  909.                 printf("%4d", digit[i]);
  910.         printf("\n");
  911. }
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.  
  929.  
  930.  
  931.  
  932.  
  933.  
  934.  
  935.  
  936.  
  937.  
  938.  
  939.  
  940.  
  941.  
  942.  
  943.  
  944.  
  945.  
  946.  
  947.  
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954.  
  955.  
  956.  
  957.  
  958. Jul 27 17:12 1984  1.91square.c Page 1
  959.  
  960.  
  961. /* example of call by value/reference */
  962.  
  963. #define MAX 10
  964. main()  /* test of square() */
  965. {
  966.         int answer[MAX], i;
  967.         square(answer, MAX);
  968.         for (i = 0; i < MAX; ++i)
  969.                 printf("element %d contains %2d\n", i, answer[i]);
  970. }
  971. square(ar, sz)  /* fills array ar of size sz with the
  972.                 /* square of the element number */
  973. int ar[], sz;
  974. {
  975.         for (--sz; sz >= 0; --sz)
  976.                 ar[sz] = sz * sz;
  977. }
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.  
  996.  
  997.  
  998.  
  999.  
  1000.  
  1001.  
  1002.  
  1003.  
  1004.  
  1005.  
  1006.  
  1007.  
  1008.  
  1009.  
  1010.  
  1011.  
  1012.  
  1013.  
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024. Jul 27 17:12 1984  1.94getline.c Page 1
  1025.  
  1026.  
  1027. /* example of character arrays */
  1028.  
  1029. #define EOF -1
  1030. #define MAX 30
  1031. main()  /* use getline() to get a name from terminal */
  1032. {
  1033.   char greet[6], name[MAX];
  1034.  
  1035.   printf("input name: ");
  1036.   if    (getline(name, MAX) > 1)
  1037.   {     greet[0] = 'H';
  1038.         greet[1] = 'E';
  1039.         greet[2] = 'L';
  1040.         greet[3] = 'L';
  1041.         greet[4] = 'O';
  1042.         greet[5] = '\0';
  1043.         printf("%s %s", greet, name);
  1044.   }
  1045.   else
  1046.         printf("no name received\n");
  1047. }
  1048. getline(line, lim)      /* function to read a line from std input;
  1049.                         /* insert NULL at end and return char count */
  1050. char    line[];         /* array where line will be stored */
  1051. int     lim;            /* max line size */
  1052. {
  1053.   int   c, i;
  1054.  
  1055.   for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
  1056.         line[i] = c;
  1057.   if (c == '\n')                /* retain  */
  1058.         line[i++] = c;          /* newline */
  1059.   line[i] = '\0'; return (i);
  1060. }
  1061.  
  1062.  
  1063.  
  1064.  
  1065.  
  1066.  
  1067.  
  1068.  
  1069.  
  1070.  
  1071.  
  1072.  
  1073.  
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090. Jul 27 17:12 1984  1.98external.c Page 1
  1091.  
  1092.  
  1093. /* example of external variables */
  1094. int max;        /* external variable */
  1095. main()
  1096. {       int i, j;
  1097.  
  1098.         i = getint();
  1099.         j = getint();
  1100.         if (findmax(i, j))
  1101.                 printf("%d is largest\n", max);
  1102.         else    printf("%d and %d are equal\n", i, j);
  1103. }
  1104. findmax(x, y)
  1105. int x, y;
  1106. {
  1107.         if      (x == y)
  1108.                 return (0);
  1109.         else if (x < y)
  1110.                 max = y;
  1111.         else    max = x;
  1112.         return (1);
  1113. }
  1114. getint()        /* function to convert ASCII
  1115.                 /* input from terminal to an int */
  1116. {       int     c, sign = 1, num = 0;
  1117.  
  1118.         printf("input integer: ");
  1119.         if      ((c = getchar()) == '-')
  1120.                 sign = -1;
  1121.         else if (c >= '0' && c <= '9')
  1122.                 num = (c - '0');
  1123.         else
  1124.                 return (0);
  1125.         while ((c = getchar()) >= '0' && c <= '9')
  1126.                 num = num * 10 + (c - '0');
  1127.         return (num * sign);
  1128. }
  1129.  
  1130.  
  1131.  
  1132.  
  1133.  
  1134.  
  1135.  
  1136.  
  1137.  
  1138.  
  1139.  
  1140.  
  1141.  
  1142.  
  1143.  
  1144.  
  1145.  
  1146.  
  1147.  
  1148.  
  1149.  
  1150.  
  1151.  
  1152.  
  1153.  
  1154. pr 2.*
  1155.  
  1156.  
  1157. Jul 27 17:12 1984  2.34conv.c Page 1
  1158.  
  1159.  
  1160. /* type conversion */
  1161.  
  1162. main()  /* print conversion table from cm to inches
  1163.         /* 1 inch = 2.54 cm - from 1 through 12 cm */
  1164. {
  1165.         int cm;
  1166.  
  1167.         for (cm = 1; cm <= 12; ++cm)
  1168.           printf("cm = %3d, inch = %6.2f\n", cm, cm / 2.54);
  1169. }
  1170.  
  1171.  
  1172.  
  1173.  
  1174.  
  1175.  
  1176.  
  1177.  
  1178.  
  1179.  
  1180.  
  1181.  
  1182.  
  1183.  
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.  
  1193.  
  1194.  
  1195.  
  1196.  
  1197.  
  1198.  
  1199.  
  1200.  
  1201.  
  1202.  
  1203.  
  1204.  
  1205.  
  1206.  
  1207.  
  1208.  
  1209.  
  1210.  
  1211.  
  1212.  
  1213.  
  1214.  
  1215.  
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223. Jul 27 17:12 1984  2.50comma.c Page 1
  1224.  
  1225.  
  1226. main()  /* calculate the sum of all integers between and including
  1227.         /* 2 input integers, compute their average and remainder */
  1228. { int   i, n1, n2, ct, sm;
  1229.   n1 = getint(); n2 = getint();
  1230.   if (n1 <= n2)
  1231.   { for (ct = sm = 0, i = n1; i <= n2; ++i)
  1232.     {   sm += i; ++ct;
  1233.     }
  1234.     printf("n1 = %d, n2 = %d, sum = %d, ave = %.2f, remainder = %d\n",
  1235.                 n1, n2, sm, (float) sm / ct, sm % ct);
  1236.   }
  1237.   else
  1238.   { printf("2nd number not big enough\n"); exit(1);
  1239.   }
  1240. }
  1241. getint()        /* function to convert ASCII
  1242.                 /* input from terminal to an int */
  1243. {       int     c, sign = 1, num = 0;
  1244.  
  1245.         printf("input integer: ");
  1246.         if      ((c = getchar()) == '-')
  1247.                 sign = -1;
  1248.         else if (c >= '0' && c <= '9')
  1249.                 num = (c - '0');
  1250.         else
  1251.                 return (0);
  1252.         while ((c = getchar()) >= '0' && c <= '9')
  1253.                 num = num * 10 + (c - '0');
  1254.         return (num * sign);
  1255. }
  1256.  
  1257.  
  1258.  
  1259.  
  1260.  
  1261.  
  1262.  
  1263.  
  1264.  
  1265.  
  1266.  
  1267.  
  1268.  
  1269.  
  1270.  
  1271.  
  1272.  
  1273.  
  1274.  
  1275.  
  1276.  
  1277.  
  1278.  
  1279.  
  1280.  
  1281.  
  1282.  
  1283.  
  1284.  
  1285.  
  1286.  
  1287.  
  1288.  
  1289. Jul 27 17:12 1984  2.58itob.c Page 1
  1290.  
  1291.  
  1292. #define SZ      8 * sizeof(int) /* for portabilty       */
  1293. main()  /* function to test itob(), integer to binary */
  1294. {       char asc[SZ+1];
  1295.         int i = getint();
  1296.         itob(i, asc);
  1297.         printf("dec = %d, octal = %o, binary = %s\n", i, i, asc);
  1298. }
  1299. itob(num, ar)   /* convert int to ASCII 0's and 1's */
  1300. int num;        /* received int */
  1301. char ar[];      /* array to store 0's and 1's */
  1302. {       int cnt, mask = 1;
  1303.         for (cnt = SZ - 1; cnt >= 0; --cnt)
  1304.         {       ar[cnt] = ((num & mask)? '1': '0'); mask <<= 1;
  1305.         }
  1306.         ar[SZ] = '\0';
  1307. }
  1308. getint()        /* function to convert ASCII
  1309.                 /* input from terminal to an int */
  1310. {       int     c, sign = 1, num = 0;
  1311.  
  1312.         printf("input integer: ");
  1313.         if      ((c = getchar()) == '-')
  1314.                 sign = -1;
  1315.         else if (c >= '0' && c <= '9')
  1316.                 num = (c - '0');
  1317.         else
  1318.                 return (0);
  1319.         while ((c = getchar()) >= '0' && c <= '9')
  1320.                 num = num * 10 + (c - '0');
  1321.         return (num * sign);
  1322. }
  1323.  
  1324.  
  1325.  
  1326.  
  1327.  
  1328.  
  1329.  
  1330.  
  1331.  
  1332.  
  1333.  
  1334.  
  1335.  
  1336.  
  1337.  
  1338.  
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344.  
  1345.  
  1346.  
  1347.  
  1348.  
  1349.  
  1350.  
  1351.  
  1352.  
  1353. pr 3.*
  1354.  
  1355.  
  1356. Jul 27 17:12 1984  3.10while.c Page 1
  1357.  
  1358.  
  1359. main()  /* example of nested while statements */
  1360. {
  1361.         int x = 10, y;
  1362.  
  1363.         while (x >= 0)
  1364.         {       y = x;
  1365.                 while (y >= 0)
  1366.                 {       printf("%2d%c", y, (y == 0)? '\n': '\040');
  1367.                         --y;
  1368.                 }
  1369.                 --x;
  1370.         }
  1371. }
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.  
  1391.  
  1392.  
  1393.  
  1394.  
  1395.  
  1396.  
  1397.  
  1398.  
  1399.  
  1400.  
  1401.  
  1402.  
  1403.  
  1404.  
  1405.  
  1406.  
  1407.  
  1408.  
  1409.  
  1410.  
  1411.  
  1412.  
  1413.  
  1414.  
  1415.  
  1416.  
  1417.  
  1418.  
  1419.  
  1420.  
  1421.  
  1422. Jul 27 17:12 1984  3.14for.c Page 1
  1423.  
  1424.  
  1425. main()  /* example of nested for statements */
  1426. {       int i, j;
  1427.  
  1428.         for (i = 0; i < 5; ++i)
  1429.         {
  1430.                 for (j = 0; j < 5; ++j)
  1431.                         printf("%d,%d  ", i, j);
  1432.                 putchar('\n');
  1433.         }
  1434. }
  1435.  
  1436.  
  1437.  
  1438.  
  1439.  
  1440.  
  1441.  
  1442.  
  1443.  
  1444.  
  1445.  
  1446.  
  1447.  
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.  
  1457.  
  1458.  
  1459.  
  1460.  
  1461.  
  1462.  
  1463.  
  1464.  
  1465.  
  1466.  
  1467.  
  1468.  
  1469.  
  1470.  
  1471.  
  1472.  
  1473.  
  1474.  
  1475.  
  1476.  
  1477.  
  1478.  
  1479.  
  1480.  
  1481.  
  1482.  
  1483.  
  1484.  
  1485.  
  1486.  
  1487.  
  1488. Jul 27 17:12 1984  3.20wc.c Page 1
  1489.  
  1490.  
  1491. #define EOF -1
  1492. main()  /* emulation of wc(1) without options */
  1493. {       int     c, inword;
  1494.         long    cc, lc, wc;
  1495.         inword = cc = lc = wc = 0;
  1496.         while ((c = getchar()) != EOF)
  1497.         {       ++cc;
  1498.                 switch (c)
  1499.                 { case '\n':    ++lc;
  1500.                   case '\t':
  1501.                   case '\040':  inword = 0; break;
  1502.                   default:      if (!inword)
  1503.                                         ++wc;
  1504.                                 inword = 1;
  1505.                 }
  1506.         }
  1507.         printf("%7ld %6ld %6ld\n", lc, wc, cc);
  1508. }
  1509.  
  1510.  
  1511.  
  1512.  
  1513.  
  1514.  
  1515.  
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.  
  1523.  
  1524.  
  1525.  
  1526.  
  1527.  
  1528.  
  1529.  
  1530.  
  1531.  
  1532.  
  1533.  
  1534.  
  1535.  
  1536.  
  1537.  
  1538.  
  1539.  
  1540.  
  1541.  
  1542.  
  1543.  
  1544.  
  1545.  
  1546.  
  1547.  
  1548.  
  1549.  
  1550.  
  1551.  
  1552.  
  1553.  
  1554. Jul 27 17:12 1984  3.24strln.c Page 1
  1555.  
  1556.  
  1557. #define EOF -1
  1558. #define MAX 256
  1559. main()  /* test strln() */
  1560. { char stor[MAX];
  1561.   getline(stor, MAX);
  1562.   printf("string length including null at end is %d\n", strln(stor));
  1563. }
  1564. strln(str)      /* return length of char string stored in array str;
  1565.                 /* include null at end in count */
  1566. char str[];
  1567. {       int i = 0, length = 0;
  1568.         do
  1569.         {       ++length;
  1570.         } while (str[i++] != '\0');
  1571.         return (length);
  1572. }
  1573. getline(line, lim)      /* function to read a line from std input;
  1574.                         /* insert NULL at end and return char count */
  1575. char    line[];         /* array where line will be stored */
  1576. int     lim;            /* max line size */
  1577. {
  1578.   int   c, i;
  1579.  
  1580.   for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
  1581.         line[i] = c;
  1582.   if (c == '\n')                /* retain  */
  1583.         line[i++] = c;          /* newline */
  1584.   line[i] = '\0'; return (i);
  1585. }
  1586.  
  1587.  
  1588.  
  1589.  
  1590.  
  1591.  
  1592.  
  1593.  
  1594.  
  1595.  
  1596.  
  1597.  
  1598.  
  1599.  
  1600.  
  1601.  
  1602.  
  1603.  
  1604.  
  1605.  
  1606.  
  1607.  
  1608.  
  1609.  
  1610.  
  1611.  
  1612.  
  1613.  
  1614.  
  1615.  
  1616.  
  1617.  
  1618.  
  1619.  
  1620. Jul 27 17:12 1984  3.28bc.c Page 1
  1621.  
  1622.  
  1623. main()  /* example of break and continue */
  1624. {
  1625.         int     x, y;
  1626.         for (x = 10; x; --x)
  1627.         {       if      (x == 3)
  1628.                         break;
  1629.                 else    y = x - 1;
  1630.                 while   (y)
  1631.                 {       if (y == 3)
  1632.                         {       --y; continue;
  1633.                         }
  1634.                         printf("%3d,%d ", x, y--);
  1635.                 }
  1636.                 putchar('\n');
  1637.         }
  1638. }
  1639.  
  1640.  
  1641.  
  1642.  
  1643.  
  1644.  
  1645.  
  1646.  
  1647.  
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.  
  1655.  
  1656.  
  1657.  
  1658.  
  1659.  
  1660.  
  1661.  
  1662.  
  1663.  
  1664.  
  1665.  
  1666.  
  1667.  
  1668.  
  1669.  
  1670.  
  1671.  
  1672.  
  1673.  
  1674.  
  1675.  
  1676.  
  1677.  
  1678.  
  1679.  
  1680.  
  1681.  
  1682.  
  1683.  
  1684.  
  1685.  
  1686. Jul 27 17:12 1984  3.32bcg.c Page 1
  1687.  
  1688.  
  1689. main()  /* example of break, continue, and goto */
  1690. {
  1691.         int     x, y;
  1692.         for (x = 10; x; --x)
  1693.         {       if      (x == 3)
  1694.                         break;
  1695.                 else    y = x - 1;
  1696.                 while   (y)
  1697.                 {       if (y == 3)
  1698.                         {       --y; continue;
  1699.                         }
  1700.                         else if (x == 5 && y == 1)
  1701.                                 goto done;
  1702.                         printf("%3d,%d ", x, y--);
  1703.                 }
  1704.                 putchar('\n');
  1705.         }
  1706.         done: printf(" DONE!\n");
  1707. }
  1708.  
  1709.  
  1710.  
  1711.  
  1712.  
  1713.  
  1714.  
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720.  
  1721.  
  1722.  
  1723.  
  1724.  
  1725.  
  1726.  
  1727.  
  1728.  
  1729.  
  1730.  
  1731.  
  1732.  
  1733.  
  1734.  
  1735.  
  1736.  
  1737.  
  1738.  
  1739.  
  1740.  
  1741.  
  1742.  
  1743.  
  1744.  
  1745.  
  1746.  
  1747.  
  1748.  
  1749.  
  1750. pr 4.*
  1751.  
  1752.  
  1753. Jul 27 17:12 1984  4.08getdbl.c Page 1
  1754.  
  1755.  
  1756. #define EOF -1
  1757. int main()      /* obtain 3 dimensions and compute volume */
  1758. {       double l, w, h, getdbl(), vol();
  1759.  
  1760.         l = getdbl(); w = getdbl(); h = getdbl();
  1761.         printf("(dimensions %.2f x %.2f x %.2f) vol is %.2f\n",
  1762.                 l, w, h, vol(l, w, h));
  1763. }
  1764. double vol(a, b, c)     /* given 3 dimensions compute volume */
  1765. double a, b, c;         /* arg declarations */
  1766. {
  1767.         return (a * b * c);
  1768. }
  1769. double  getdbl()        /* convert ASCII from terminal to dbl */
  1770. {       int     c, sign = 1;    /* c = char input, sign = + */
  1771.         float   d = 1.0;        /* d = decimal place */
  1772.         double  num = 0.0;      /* num = converted number */
  1773.         printf("input number: ");
  1774.         if      ((c = getchar()) == '-')
  1775.                 sign = -1;                      /* sign = - */
  1776.         else if (c == '\n' || c == EOF)
  1777.                 return (0);
  1778.         else if (c == '.')
  1779.                 goto dot;
  1780.         else if (c >= '0' && c <= '9')
  1781.                 num = (c - '0');
  1782.         while ((c = getchar()) != '\n' && c != '.' && c != EOF)
  1783.                 if (c >= '0' && c <= '9')
  1784.                         num = num * 10 + (c - '0');
  1785.         if (c == '.')
  1786. dot:            while ((c = getchar()) != EOF && c != '\n')
  1787.                         if (c >= '0' && c <= '9')
  1788.                         { d /= 10.0; num += d * (c - '0');
  1789.                         }
  1790.         return (num * sign);
  1791. }
  1792.  
  1793.  
  1794.  
  1795.  
  1796.  
  1797.  
  1798.  
  1799.  
  1800.  
  1801.  
  1802.  
  1803.  
  1804.  
  1805.  
  1806.  
  1807.  
  1808.  
  1809.  
  1810.  
  1811.  
  1812.  
  1813.  
  1814.  
  1815.  
  1816.  
  1817.  
  1818.  
  1819. Jul 27 17:12 1984  4.14vol_a.c Page 1
  1820.  
  1821.  
  1822. double l, w, h, volume; /* external variables */
  1823.  
  1824. int main()      /* obtain 3 dimensions and compute volume */
  1825. {
  1826.         double getdbl();
  1827.         l = getdbl(); w = getdbl(); h = getdbl();
  1828.         vol_ext();
  1829.         printf("(dimensions %.2f x %.2f x %.2f) vol is %.2f\n",
  1830.                 l, w, h, volume);
  1831. }
  1832.  
  1833.  
  1834.  
  1835.  
  1836.  
  1837.  
  1838.  
  1839.  
  1840.  
  1841.  
  1842.  
  1843.  
  1844.  
  1845.  
  1846.  
  1847.  
  1848.  
  1849.  
  1850.  
  1851.  
  1852.  
  1853.  
  1854.  
  1855.  
  1856.  
  1857.  
  1858.  
  1859.  
  1860.  
  1861.  
  1862.  
  1863.  
  1864.  
  1865.  
  1866.  
  1867.  
  1868.  
  1869.  
  1870.  
  1871.  
  1872.  
  1873.  
  1874.  
  1875.  
  1876.  
  1877.  
  1878.  
  1879.  
  1880.  
  1881.  
  1882.  
  1883.  
  1884.  
  1885. Jul 27 17:12 1984  4.14vol_b.c Page 1
  1886.  
  1887.  
  1888. extern double l, w, h, volume;  /* extern required if sep src file */
  1889.  
  1890. vol_ext()       /* given 3 dimensions compute volume */
  1891. {
  1892.         volume = l * w * h;
  1893. }
  1894.  
  1895.  
  1896.  
  1897.  
  1898.  
  1899.  
  1900.  
  1901.  
  1902.  
  1903.  
  1904.  
  1905.  
  1906.  
  1907.  
  1908.  
  1909.  
  1910.  
  1911.  
  1912.  
  1913.  
  1914.  
  1915.  
  1916.  
  1917.  
  1918.  
  1919.  
  1920.  
  1921.  
  1922.  
  1923.  
  1924.  
  1925.  
  1926.  
  1927.  
  1928.  
  1929.  
  1930.  
  1931.  
  1932.  
  1933.  
  1934.  
  1935.  
  1936.  
  1937.  
  1938.  
  1939.  
  1940.  
  1941.  
  1942.  
  1943.  
  1944.  
  1945.  
  1946.  
  1947.  
  1948.  
  1949.  
  1950.  
  1951. Jul 27 17:12 1984  4.14vol_c.c Page 1
  1952.  
  1953.  
  1954. #define EOF -1          /* define moved to this file */
  1955. double  getdbl()        /* convert ASCII from terminal to dbl */
  1956. {       int     c, sign = 1;    /* c = char input, sign = + */
  1957.         float   d = 1.0;        /* d = decimal place */
  1958.         double  num = 0.0;      /* num = converted number */
  1959.         printf("input number: ");
  1960.         if      ((c = getchar()) == '-')
  1961.                 sign = -1;                      /* sign = - */
  1962.         else if (c == '\n' || c == EOF)
  1963.                 return (0);
  1964.         else if (c == '.')
  1965.                 goto dot;
  1966.         else if (c >= '0' && c <= '9')
  1967.                 num = (c - '0');
  1968.         while ((c = getchar()) != '\n' && c != '.' && c != EOF)
  1969.                 if (c >= '0' && c <= '9')
  1970.                         num = num * 10 + (c - '0');
  1971.         if (c == '.')
  1972. dot:            while ((c = getchar()) != EOF && c != '\n')
  1973.                         if (c >= '0' && c <= '9')
  1974.                         { d /= 10.0; num += d * (c - '0');
  1975.                         }
  1976.         return (num * sign);
  1977. }
  1978.  
  1979.  
  1980.  
  1981.  
  1982.  
  1983.  
  1984.  
  1985.  
  1986.  
  1987.  
  1988.  
  1989.  
  1990.  
  1991.  
  1992.  
  1993.  
  1994.  
  1995.  
  1996.  
  1997.  
  1998.  
  1999.  
  2000.  
  2001.  
  2002.  
  2003.  
  2004.  
  2005.  
  2006.  
  2007.  
  2008.  
  2009.  
  2010.  
  2011.  
  2012.  
  2013.  
  2014.  
  2015.  
  2016.  
  2017. Jul 27 17:12 1984  4.17perm.c Page 1
  2018.  
  2019.  
  2020. main()  /* test stat() */
  2021. {
  2022.         int i;
  2023.         for (i = 0; i <= 9; ++i)
  2024.                 stat();
  2025. }
  2026. stat()  /* demonstration of internal static */
  2027. {
  2028.         int fgt = 0;
  2029.         static int rem;         /* count calls */
  2030.  
  2031.         printf("this is call %2d, fgt is %d\n", ++rem, ++fgt);
  2032. }
  2033.  
  2034.  
  2035.  
  2036.  
  2037.  
  2038.  
  2039.  
  2040.  
  2041.  
  2042.  
  2043.  
  2044.  
  2045.  
  2046.  
  2047.  
  2048.  
  2049.  
  2050.  
  2051.  
  2052.  
  2053.  
  2054.  
  2055.  
  2056.  
  2057.  
  2058.  
  2059.  
  2060.  
  2061.  
  2062.  
  2063.  
  2064.  
  2065.  
  2066.  
  2067.  
  2068.  
  2069.  
  2070.  
  2071.  
  2072.  
  2073.  
  2074.  
  2075.  
  2076.  
  2077.  
  2078.  
  2079.  
  2080.  
  2081.  
  2082.  
  2083. Jul 27 17:12 1984  4.18block.c Page 1
  2084.  
  2085.  
  2086. char ABC[] = { 'A', 'B', 'C', '\0' };                           /* 1st declaration */
  2087. main()  /* demonstration of static and scope rules */
  2088. { printf("main() ABC[] is %s\n", ABC);
  2089.   sub(); sub();
  2090. }
  2091. sub()   /* declare, initialize, & access two ABC[]'s */
  2092. { static int ABC[] = { 1, 2, 3 };                               /* 2nd declaration */
  2093.   { static long ABC[] = { 10000, 20000, 30000 };                /* 3rd declaration */
  2094.     printf("sub() sub-block ABC[1] is %ld\n", ABC[1]++);
  2095.   }
  2096.   printf("sub() ABC[1] is %d\n", ABC[1]++);
  2097. }
  2098.  
  2099.  
  2100.  
  2101.  
  2102.  
  2103.  
  2104.  
  2105.  
  2106.  
  2107.  
  2108.  
  2109.  
  2110.  
  2111.  
  2112.  
  2113.  
  2114.  
  2115.  
  2116.  
  2117.  
  2118.  
  2119.  
  2120.  
  2121.  
  2122.  
  2123.  
  2124.  
  2125.  
  2126.  
  2127.  
  2128.  
  2129.  
  2130.  
  2131.  
  2132.  
  2133.  
  2134.  
  2135.  
  2136.  
  2137.  
  2138.  
  2139.  
  2140.  
  2141.  
  2142.  
  2143.  
  2144.  
  2145.  
  2146.  
  2147.  
  2148.  
  2149. Jul 27 17:12 1984  4.26fact.c Page 1
  2150.  
  2151.  
  2152. /* example of recursive function */
  2153.  
  2154. main() /* test fact() for range 1 - 10 */
  2155. {       register int i;
  2156.         double fact();
  2157.         for (i = 1; i <= 10; ++i)
  2158.                 printf("%2d! is %8.0f\n", i, fact(i));
  2159. }
  2160. double fact(num)        /* recursive function to compute and
  2161.                            return factorial (num!) for num > 0 */
  2162. int num;
  2163. {       if      (num > 1)
  2164.                 return (num * fact(num - 1));
  2165.         else    return 1;
  2166. }
  2167.  
  2168.  
  2169.  
  2170.  
  2171.  
  2172.  
  2173.  
  2174.  
  2175.  
  2176.  
  2177.  
  2178.  
  2179.  
  2180.  
  2181.  
  2182.  
  2183.  
  2184.  
  2185.  
  2186.  
  2187.  
  2188.  
  2189.  
  2190.  
  2191.  
  2192.  
  2193.  
  2194.  
  2195.  
  2196.  
  2197.  
  2198.  
  2199.  
  2200.  
  2201.  
  2202.  
  2203.  
  2204.  
  2205.  
  2206.  
  2207.  
  2208.  
  2209.  
  2210.  
  2211.  
  2212.  
  2213.  
  2214.  
  2215. Jul 27 17:12 1984  4.29prepro.c Page 1
  2216.  
  2217.  
  2218. /* examples of #include & #define */
  2219.  
  2220. #include <stdio.h>      /* EOF - used by getdbl() -
  2221.                         /* is defined in <stdio.h> */
  2222. #include "getdbl.f"     /* assumes getdbl() is in
  2223.                         /* file getdbl.f in wd */
  2224. #define max(A, B) ((A) > (B)? (A): (B))
  2225.  
  2226. main()  /* test max macro */
  2227. {
  2228.         double getdbl();
  2229.         double i = getdbl(), j = getdbl();
  2230.         printf("max is %f\n", max(i, j));
  2231. }
  2232.  
  2233.  
  2234.  
  2235.  
  2236.  
  2237.  
  2238.  
  2239.  
  2240.  
  2241.  
  2242.  
  2243.  
  2244.  
  2245.  
  2246.  
  2247.  
  2248.  
  2249.  
  2250.  
  2251.  
  2252.  
  2253.  
  2254.  
  2255.  
  2256.  
  2257.  
  2258.  
  2259.  
  2260.  
  2261.  
  2262.  
  2263.  
  2264.  
  2265.  
  2266.  
  2267.  
  2268.  
  2269.  
  2270.  
  2271.  
  2272.  
  2273.  
  2274.  
  2275.  
  2276.  
  2277.  
  2278.  
  2279.  
  2280.  
  2281. Jul 27 17:12 1984  4.30sqrt.c Page 1
  2282.  
  2283.  
  2284. /* another #include and nested function calls
  2285. /* compile as "cc filename -lm"
  2286. /* see exp(3M) and start of section 3 (3M) */
  2287.  
  2288. #include <math.h>       /* declares sqrt() as type double */
  2289. #include <stdio.h>      /* provides EOF for getdbl() */
  2290. #include "getdbl.f"     /* assumes getdbl() is in
  2291.                         /* file getdbl.f in wd */
  2292.  
  2293. main() /* use sqrt() to compute square roots */
  2294. {
  2295.         double getdbl();
  2296.         printf("square root is %f\n", sqrt(getdbl()));
  2297. }
  2298.  
  2299.  
  2300.  
  2301.  
  2302.  
  2303.  
  2304.  
  2305.  
  2306.  
  2307.  
  2308.  
  2309.  
  2310.  
  2311.  
  2312.  
  2313.  
  2314.  
  2315.  
  2316.  
  2317.  
  2318.  
  2319.  
  2320.  
  2321.  
  2322.  
  2323.  
  2324.  
  2325.  
  2326.  
  2327.  
  2328.  
  2329.  
  2330.  
  2331.  
  2332.  
  2333.  
  2334.  
  2335.  
  2336.  
  2337.  
  2338.  
  2339.  
  2340.  
  2341.  
  2342.  
  2343.  
  2344.  
  2345. pr 5.*
  2346.  
  2347.  
  2348. Aug  3 09:13 1984  5.12pdemo.c Page 1
  2349.  
  2350.  
  2351. main()          /* demonstration of pointers */
  2352. {
  2353.   int x, y;
  2354.   int *px;      /* declare px pointer to type int */
  2355.  
  2356.   x = 5;
  2357.   px = &x;      /* initialize px to address of x */
  2358.   y = *px;      /* initialize y to contents of address
  2359.                    contained in px */
  2360.   printf("addresses of x, y, px are...\n\t%8o, %8o, %8o\n",
  2361.                 &x, &y, &px);
  2362.   printf("values of x, y, px, *px are...\n\t%8d, %8d, %8o, %8d\n",
  2363.                 x, y, px, *px);
  2364. y = 12;
  2365. px = &y;
  2366.   printf("addresses of x, y, px are...\n\t%8o, %8o, %8o\n",
  2367.                 &x, &y, &px);
  2368.   printf("values of x, y, px, *px are...\n\t%8d, %8d, %8o, %8d\n",
  2369.                 x, y, px, *px);
  2370. }
  2371.  
  2372.  
  2373.  
  2374.  
  2375.  
  2376.  
  2377.  
  2378.  
  2379.  
  2380.  
  2381.  
  2382.  
  2383.  
  2384.  
  2385.  
  2386.  
  2387.  
  2388.  
  2389.  
  2390.  
  2391.  
  2392.  
  2393.  
  2394.  
  2395.  
  2396.  
  2397.  
  2398.  
  2399.  
  2400.  
  2401.  
  2402.  
  2403.  
  2404.  
  2405.  
  2406.  
  2407.  
  2408.  
  2409.  
  2410.  
  2411.  
  2412.  
  2413.  
  2414. Jul 27 17:12 1984  5.14swap_ng.c Page 1
  2415.  
  2416.  
  2417. /* swapping values - wrong way */
  2418.  
  2419. #include "getint.f"     /* assumes getint() in
  2420.                            file getint.f in wd */
  2421. main()
  2422. {       int a = getint(), b = getint();
  2423.  
  2424.         printf("original a, b are\t%6d, %6d\n", a, b);
  2425.         swap_ng(a, b);
  2426.         printf("swapped a, b are\t%6d, %6d\n", a, b);
  2427. }
  2428. swap_ng(x, y)           /* call by value */
  2429. int x, y;
  2430. {       int tmp;
  2431.         tmp = x;
  2432.         x = y;
  2433.         y = tmp;
  2434. }
  2435.  
  2436.  
  2437.  
  2438.  
  2439.  
  2440.  
  2441.  
  2442.  
  2443.  
  2444.  
  2445.  
  2446.  
  2447.  
  2448.  
  2449.  
  2450.  
  2451.  
  2452.  
  2453.  
  2454.  
  2455.  
  2456.  
  2457.  
  2458.  
  2459.  
  2460.  
  2461.  
  2462.  
  2463.  
  2464.  
  2465.  
  2466.  
  2467.  
  2468.  
  2469.  
  2470.  
  2471.  
  2472.  
  2473.  
  2474.  
  2475.  
  2476.  
  2477.  
  2478.  
  2479.  
  2480. Jul 27 17:12 1984  5.17swap.c Page 1
  2481.  
  2482.  
  2483. /* swapping values - correct way */
  2484.  
  2485. #include "getint.f"     /* assumes getint() in
  2486.                            file getint.f in wd */
  2487. main()
  2488. {       int a = getint(), b = getint();
  2489.  
  2490.         printf("original a, b are\t%6d, %6d\n", a, b);
  2491.         swap(&a, &b);
  2492.         printf("swapped a, b are\t%6d, %6d\n", a, b);
  2493. }
  2494. swap(px, py)            /* call by reference */
  2495. int *px, *py;           /* px, py are pointers to type int */
  2496. {       int tmp;
  2497.         tmp = *px;
  2498.         *px = *py;
  2499.         *py = tmp;
  2500. }
  2501.  
  2502.  
  2503.  
  2504.  
  2505.  
  2506.  
  2507.  
  2508.  
  2509.  
  2510.  
  2511.  
  2512.  
  2513.  
  2514.  
  2515.  
  2516.  
  2517.  
  2518.  
  2519.  
  2520.  
  2521.  
  2522.  
  2523.  
  2524.  
  2525.  
  2526.  
  2527.  
  2528.  
  2529.  
  2530.  
  2531.  
  2532.  
  2533.  
  2534.  
  2535.  
  2536.  
  2537.  
  2538.  
  2539.  
  2540.  
  2541.  
  2542.  
  2543.  
  2544.  
  2545.  
  2546. Jul 27 17:12 1984  5.20ap.c Page 1
  2547.  
  2548.  
  2549. /* demonstration of similarity between arrays and pointers */
  2550.  
  2551. main()
  2552. {       static char ar[] = "test";
  2553.         char *par;
  2554.         int i;
  2555.         par = ar;               /* "&" not used, WHY? */
  2556.  
  2557.         printf("array is\t\"%s\"\n", ar);
  2558.         printf("pointer is\t\"%s\"\n", par);
  2559.         printf("array\tpointer\n");
  2560.         for (i = 0; i < (sizeof(ar) - 1); ++i)
  2561.                 printf("  %c\t  %c\n", ar[i], *par++);
  2562. }
  2563.  
  2564.  
  2565.  
  2566.  
  2567.  
  2568.  
  2569.  
  2570.  
  2571.  
  2572.  
  2573.  
  2574.  
  2575.  
  2576.  
  2577.  
  2578.  
  2579.  
  2580.  
  2581.  
  2582.  
  2583.  
  2584.  
  2585.  
  2586.  
  2587.  
  2588.  
  2589.  
  2590.  
  2591.  
  2592.  
  2593.  
  2594.  
  2595.  
  2596.  
  2597.  
  2598.  
  2599.  
  2600.  
  2601.  
  2602.  
  2603.  
  2604.  
  2605.  
  2606.  
  2607.  
  2608.  
  2609.  
  2610.  
  2611.  
  2612. Jul 27 17:12 1984  5.26pcs.c Page 1
  2613.  
  2614.  
  2615. /* pointers to "character strings" */
  2616.  
  2617. main()
  2618. {       static char arr1[] = { 'I', ' ', 'u', 's', 'e', '\0' };
  2619.         char *ptr1 = arr1;
  2620.         char *ptr2 = "pointers.";
  2621.  
  2622.         printf("%s %s\n", ptr1, ptr2);
  2623. }
  2624.  
  2625.  
  2626.  
  2627.  
  2628.  
  2629.  
  2630.  
  2631.  
  2632.  
  2633.  
  2634.  
  2635.  
  2636.  
  2637.  
  2638.  
  2639.  
  2640.  
  2641.  
  2642.  
  2643.  
  2644.  
  2645.  
  2646.  
  2647.  
  2648.  
  2649.  
  2650.  
  2651.  
  2652.  
  2653.  
  2654.  
  2655.  
  2656.  
  2657.  
  2658.  
  2659.  
  2660.  
  2661.  
  2662.  
  2663.  
  2664.  
  2665.  
  2666.  
  2667.  
  2668.  
  2669.  
  2670.  
  2671.  
  2672.  
  2673.  
  2674.  
  2675.  
  2676.  
  2677.  
  2678. Jul 27 17:12 1984  5.28strln_p.c Page 1
  2679.  
  2680.  
  2681. /* pointer version of strln() */
  2682.  
  2683. #define MAX 256
  2684. #include <stdio.h>      /* getline() needs EOF */
  2685. #include "getline.f"    /* assumes getline() is in
  2686.                            file getline.f in wd */
  2687. main()  /* test strln_p() */
  2688. {       char stor[MAX];
  2689.         getline(stor, MAX);
  2690.         printf("string length is %d\n", strln_p(stor));
  2691. }
  2692. strln_p(ptr)    /* return length of char string pointed to by ptr;
  2693.                 /* include null at end in count */
  2694. char *ptr;
  2695. {       register length = 0;
  2696.         do
  2697.         {       ++length;
  2698.         } while (*ptr++ != '\0');       /* ++ increments ptr by 1 */
  2699.         return (length);
  2700. }
  2701.  
  2702.  
  2703.  
  2704.  
  2705.  
  2706.  
  2707.  
  2708.  
  2709.  
  2710.  
  2711.  
  2712.  
  2713.  
  2714.  
  2715.  
  2716.  
  2717.  
  2718.  
  2719.  
  2720.  
  2721.  
  2722.  
  2723.  
  2724.  
  2725.  
  2726.  
  2727.  
  2728.  
  2729.  
  2730.  
  2731.  
  2732.  
  2733.  
  2734.  
  2735.  
  2736.  
  2737.  
  2738.  
  2739.  
  2740.  
  2741.  
  2742.  
  2743.  
  2744. Jul 27 17:12 1984  5.30strcpy.c Page 1
  2745.  
  2746.  
  2747. /* copying strings - array version */
  2748.  
  2749. #define MAX 256
  2750. #include <stdio.h>      /* getline() needs EOF */
  2751. #include "getline.f"    /* assumes getline() is in
  2752.                            file getline.f in wd */
  2753. main() /* test strcpy() */
  2754. {       char src[MAX], dest[MAX];
  2755.         getline(src, MAX);
  2756.         strcpy(dest, src);
  2757.         printf("src, dest...\n%s%s", src, dest);
  2758. }
  2759. strcpy(d, s)    /* copy string in s[] into d[] */
  2760. char d[], s[];
  2761. {       register i;
  2762.         for (i = 0; d[i] = s[i]; ++i)
  2763.                 ;
  2764. }
  2765.  
  2766.  
  2767.  
  2768.  
  2769.  
  2770.  
  2771.  
  2772.  
  2773.  
  2774.  
  2775.  
  2776.  
  2777.  
  2778.  
  2779.  
  2780.  
  2781.  
  2782.  
  2783.  
  2784.  
  2785.  
  2786.  
  2787.  
  2788.  
  2789.  
  2790.  
  2791.  
  2792.  
  2793.  
  2794.  
  2795.  
  2796.  
  2797.  
  2798.  
  2799.  
  2800.  
  2801.  
  2802.  
  2803.  
  2804.  
  2805.  
  2806.  
  2807.  
  2808.  
  2809.  
  2810. Jul 27 17:12 1984  5.33strcpy_p.c Page 1
  2811.  
  2812.  
  2813. /* copying strings - pointer version */
  2814.  
  2815. #define MAX 256
  2816. #include <stdio.h>      /* getline() needs EOF */
  2817. #include "getline.f"    /* assumes getline() is in
  2818.                            file getline.f in wd */
  2819. main() /* test strcpy_p() */
  2820. {       char src[MAX], dest[MAX];
  2821.         getline(src, MAX);
  2822.         strcpy_p(dest, src);
  2823.         printf("src, dest...\n%s%s", src, dest);
  2824. }
  2825. strcpy_p(d, s)  /* copy string *s to *d */
  2826. char *d, *s;
  2827. {       
  2828.         while (*d++ = *s++)
  2829.                 ;
  2830. }
  2831.  
  2832.  
  2833.  
  2834.  
  2835.  
  2836.  
  2837.  
  2838.  
  2839.  
  2840.  
  2841.  
  2842.  
  2843.  
  2844.  
  2845.  
  2846.  
  2847.  
  2848.  
  2849.  
  2850.  
  2851.  
  2852.  
  2853.  
  2854.  
  2855.  
  2856.  
  2857.  
  2858.  
  2859.  
  2860.  
  2861.  
  2862.  
  2863.  
  2864.  
  2865.  
  2866.  
  2867.  
  2868.  
  2869.  
  2870.  
  2871.  
  2872.  
  2873.  
  2874.  
  2875.  
  2876. Jul 27 17:12 1984  5.34strcmp.c Page 1
  2877.  
  2878.  
  2879. /* comparing strings with pointers */
  2880.  
  2881. #define MAX 256
  2882. #include <stdio.h>
  2883. #include "getline.f"
  2884. main()  /* test strcmp() */
  2885. {       char str1[MAX], str2[MAX];
  2886.         register ret;
  2887.         printf("input 1st string: "); getline(str1, MAX);
  2888.         printf("input 2nd string: "); getline(str2, MAX);
  2889.         if (!(ret = strcmp(str1, str2)))
  2890.                 printf("they are equal\n");
  2891.         else    printf("no match, char %d\n", ret);
  2892. }
  2893. strcmp(s1, s2)  /* compare 2 strings;
  2894.                 /* if match return 0 (else char count) */
  2895. char    *s1, *s2;
  2896. {       register i;
  2897.         for (i = 1; *s1 == *s2; ++i)
  2898.                 if (*s1++ == '\0')
  2899.                         return (0);
  2900.                 else s2++;
  2901.         return (i);
  2902. }
  2903.  
  2904.  
  2905.  
  2906.  
  2907.  
  2908.  
  2909.  
  2910.  
  2911.  
  2912.  
  2913.  
  2914.  
  2915.  
  2916.  
  2917.  
  2918.  
  2919.  
  2920.  
  2921.  
  2922.  
  2923.  
  2924.  
  2925.  
  2926.  
  2927.  
  2928.  
  2929.  
  2930.  
  2931.  
  2932.  
  2933.  
  2934.  
  2935.  
  2936.  
  2937.  
  2938.  
  2939.  
  2940.  
  2941.  
  2942. Jul 27 17:12 1984  5.38mda1.c Page 1
  2943.  
  2944.  
  2945. #define D1 3
  2946. #define D2 5
  2947. int a[D1][D2];  /* two-dimensional array */
  2948. main()          /* demonstration of multi-dimensional array */
  2949. { register i, j;
  2950.  
  2951.   for (i = 0; i < D1; ++i)
  2952.     for (j = 0; j < D2; ++j)
  2953.     { a[i][j] = (i * D2) + j + 1;
  2954.       printf("a[%d][%d] adr, value is %o, %2d\n",
  2955.                 i, j, &a[i][j], a[i][j]);
  2956.     }
  2957. }
  2958.  
  2959.  
  2960.  
  2961.  
  2962.  
  2963.  
  2964.  
  2965.  
  2966.  
  2967.  
  2968.  
  2969.  
  2970.  
  2971.  
  2972.  
  2973.  
  2974.  
  2975.  
  2976.  
  2977.  
  2978.  
  2979.  
  2980.  
  2981.  
  2982.  
  2983.  
  2984.  
  2985.  
  2986.  
  2987.  
  2988.  
  2989.  
  2990.  
  2991.  
  2992.  
  2993.  
  2994.  
  2995.  
  2996.  
  2997.  
  2998.  
  2999.  
  3000.  
  3001.  
  3002.  
  3003.  
  3004.  
  3005.  
  3006.  
  3007.  
  3008. Jul 27 17:12 1984  5.40mda2.c Page 1
  3009.  
  3010.  
  3011. #define D1 3
  3012. #define D2 5
  3013. int a[][D2] =   { {  1,  2,  3,  4,  5 },
  3014.                   {  6,  7,  8,  9, 10 },
  3015.                   { 11, 12, 13, 14, 15 }
  3016.                 };      /* compiler initialized */
  3017. main()  /* two-dimensional array; another version */
  3018. { register i, j;
  3019.  
  3020.   for (i = 0; i < D1; ++i)
  3021.     for (j = 0; j < D2; ++j)
  3022.       printf("a[%d][%d] adr, value is %o, %2d\n",
  3023.                 i, j, &a[i][j], a[i][j]);
  3024. }
  3025.  
  3026.  
  3027.  
  3028.  
  3029.  
  3030.  
  3031.  
  3032.  
  3033.  
  3034.  
  3035.  
  3036.  
  3037.  
  3038.  
  3039.  
  3040.  
  3041.  
  3042.  
  3043.  
  3044.  
  3045.  
  3046.  
  3047.  
  3048.  
  3049.  
  3050.  
  3051.  
  3052.  
  3053.  
  3054.  
  3055.  
  3056.  
  3057.  
  3058.  
  3059.  
  3060.  
  3061.  
  3062.  
  3063.  
  3064.  
  3065.  
  3066.  
  3067.  
  3068.  
  3069.  
  3070.  
  3071.  
  3072.  
  3073.  
  3074. Jul 27 17:12 1984  5.44aop.c Page 1
  3075.  
  3076.  
  3077. main()  /* demonstration of an array of pointers */
  3078. {       register i;
  3079.         static char *arp[] =    { "I",
  3080.                                   "NEED",
  3081.                                   "HELP",
  3082.                                   ""
  3083.                                 };
  3084.         for (i = 0; *arp[i]; ++i)
  3085.                 printf("%s ", arp[i]);  
  3086.         putchar('\n');
  3087. }
  3088.  
  3089.  
  3090.  
  3091.  
  3092.  
  3093.  
  3094.  
  3095.  
  3096.  
  3097.  
  3098.  
  3099.  
  3100.  
  3101.  
  3102.  
  3103.  
  3104.  
  3105.  
  3106.  
  3107.  
  3108.  
  3109.  
  3110.  
  3111.  
  3112.  
  3113.  
  3114.  
  3115.  
  3116.  
  3117.  
  3118.  
  3119.  
  3120.  
  3121.  
  3122.  
  3123.  
  3124.  
  3125.  
  3126.  
  3127.  
  3128.  
  3129.  
  3130.  
  3131.  
  3132.  
  3133.  
  3134.  
  3135.  
  3136.  
  3137.  
  3138.  
  3139.  
  3140. Jul 27 17:12 1984  5.49argdemo.c Page 1
  3141.  
  3142.  
  3143. /* demonstration of arguments passed to main() */
  3144.  
  3145. main(argc, argv, envp)          /* three args to main() */
  3146. int     argc;                   /* argc is an int */
  3147. char    *argv[], *envp[];       /* argv & envp are arrays
  3148.                                 /* of character pointers */
  3149. {       register i;
  3150.  
  3151.         printf("argc is %d\n", argc);
  3152.         for (i = 0; i < argc; ++i)
  3153.                 printf("*argv[%d] is %s\n", i, argv[i]);
  3154.         for (i = 0; envp[i]; ++i)
  3155.                 printf("*envp[%d] is %s\n", i, envp[i]);
  3156. }
  3157.  
  3158.  
  3159.  
  3160.  
  3161.  
  3162.  
  3163.  
  3164.  
  3165.  
  3166.  
  3167.  
  3168.  
  3169.  
  3170.  
  3171.  
  3172.  
  3173.  
  3174.  
  3175.  
  3176.  
  3177.  
  3178.  
  3179.  
  3180.  
  3181.  
  3182.  
  3183.  
  3184.  
  3185.  
  3186.  
  3187.  
  3188.  
  3189.  
  3190.  
  3191.  
  3192.  
  3193.  
  3194.  
  3195.  
  3196.  
  3197.  
  3198.  
  3199.  
  3200.  
  3201.  
  3202.  
  3203.  
  3204.  
  3205.  
  3206. Jul 27 17:12 1984  5.50echo1.c Page 1
  3207.  
  3208.  
  3209. main(argc, argv)        /* emulation of basic echo(1) command;
  3210.                         /* first version, array notation */
  3211. int     argc;
  3212. char    *argv[];
  3213. {
  3214.   register i;
  3215.  
  3216.   for (i = 1; i < argc; ++i)
  3217.     printf("%s%c", argv[i], (i < argc - 1)? ' ': '\n');
  3218. }
  3219.  
  3220.  
  3221.  
  3222.  
  3223.  
  3224.  
  3225.  
  3226.  
  3227.  
  3228.  
  3229.  
  3230.  
  3231.  
  3232.  
  3233.  
  3234.  
  3235.  
  3236.  
  3237.  
  3238.  
  3239.  
  3240.  
  3241.  
  3242.  
  3243.  
  3244.  
  3245.  
  3246.  
  3247.  
  3248.  
  3249.  
  3250.  
  3251.  
  3252.  
  3253.  
  3254.  
  3255.  
  3256.  
  3257.  
  3258.  
  3259.  
  3260.  
  3261.  
  3262.  
  3263.  
  3264.  
  3265.  
  3266.  
  3267.  
  3268.  
  3269.  
  3270.  
  3271.  
  3272. Jul 27 17:12 1984  5.53echo2.c Page 1
  3273.  
  3274.  
  3275. main(argc, argv)        /* emulation of basic echo(1) command;
  3276.                         /* second version, pointer notation */
  3277. int     argc;
  3278. char    **argv;         /* same as *argv[] */
  3279. {
  3280.   while (--argc > 0)
  3281.     printf("%s%c", *++argv, (argc > 1)? ' ': '\n');
  3282. }
  3283.  
  3284.  
  3285.  
  3286.  
  3287.  
  3288.  
  3289.  
  3290.  
  3291.  
  3292.  
  3293.  
  3294.  
  3295.  
  3296.  
  3297.  
  3298.  
  3299.  
  3300.  
  3301.  
  3302.  
  3303.  
  3304.  
  3305.  
  3306.  
  3307.  
  3308.  
  3309.  
  3310.  
  3311.  
  3312.  
  3313.  
  3314.  
  3315.  
  3316.  
  3317.  
  3318.  
  3319.  
  3320.  
  3321.  
  3322.  
  3323.  
  3324.  
  3325.  
  3326.  
  3327.  
  3328.  
  3329.  
  3330.  
  3331.  
  3332.  
  3333.  
  3334.  
  3335.  
  3336.  
  3337.  
  3338. Jul 27 17:12 1984  5.60frp.c Page 1
  3339.  
  3340.  
  3341. /* demonstration of function returning pointer */
  3342.  
  3343. main()                  /* test new version of getint() */
  3344. {
  3345.   int *getintp();       /* getintp() returns pointer to type int */
  3346.  
  3347.   printf("%d\n", *getintp());
  3348. }
  3349. int *getintp()  /* function to convert ASCII
  3350.                 /* input from terminal to an int;
  3351.                 /* return pointer to int */
  3352. { int   c, sign = 1, num = 0;
  3353.  
  3354.   printf("input integer: ");
  3355.   if    ((c = getchar()) == '-')
  3356.         sign = -1;
  3357.   else if (c >= '0' && c <= '9')
  3358.         num = (c - '0');
  3359.   else
  3360.         return (&num);
  3361.   while ((c = getchar()) >= '0' && c <= '9')
  3362.         num = num * 10 + (c - '0');
  3363.   num *= sign;
  3364.   return (&num);
  3365. }
  3366.  
  3367.  
  3368.  
  3369.  
  3370.  
  3371.  
  3372.  
  3373.  
  3374.  
  3375.  
  3376.  
  3377.  
  3378.  
  3379.  
  3380.  
  3381.  
  3382.  
  3383.  
  3384.  
  3385.  
  3386.  
  3387.  
  3388.  
  3389.  
  3390.  
  3391.  
  3392.  
  3393.  
  3394.  
  3395.  
  3396.  
  3397.  
  3398.  
  3399.  
  3400.  
  3401.  
  3402.  
  3403.  
  3404. Jul 27 17:12 1984  5.63pfi.c Page 1
  3405.  
  3406.  
  3407. /* demonstration of pointer to function */
  3408.  
  3409. main()          /* test inter() */
  3410. {
  3411.   int getint(); /* getint() must be declared */
  3412.  
  3413.   printf("%d\n", inter(getint));
  3414. }
  3415. inter(pfi)      /* call function via pointer arg */
  3416. int (*pfi)();   /* pfi is pointer to function
  3417.                 /* returning type int */
  3418. {
  3419.   return ((*pfi)());    /* (*pfi)() is a function call */
  3420. }
  3421. #include "getint.f"     /* assumes getint() is in
  3422.                         /* file getint.f in wd */
  3423.  
  3424.  
  3425.  
  3426.  
  3427.  
  3428.  
  3429.  
  3430.  
  3431.  
  3432.  
  3433.  
  3434.  
  3435.  
  3436.  
  3437.  
  3438.  
  3439.  
  3440.  
  3441.  
  3442.  
  3443.  
  3444.  
  3445.  
  3446.  
  3447.  
  3448.  
  3449.  
  3450.  
  3451.  
  3452.  
  3453.  
  3454.  
  3455.  
  3456.  
  3457.  
  3458.  
  3459.  
  3460.  
  3461.  
  3462.  
  3463.  
  3464.  
  3465.  
  3466.  
  3467.  
  3468. Bpr 6.*
  3469.  
  3470.  
  3471. Jul 27 17:12 1984  6.14struct1.c Page 1
  3472.  
  3473.  
  3474. /* demonstration of a structure, 1st version */
  3475.  
  3476. struct data
  3477. { char first[20];
  3478.   char  last[26];
  3479.   double     sal;
  3480. };                      /* external definition */
  3481. main()                  /* print structure contents */
  3482. { static struct data emp = { "Linda",
  3483.                              "Sample",
  3484.                               39000.0
  3485.                            };   /* declare and init */
  3486.   printf("%s %s earns $%.2f per year\n",
  3487.                 emp.first, emp.last, emp.sal);
  3488. }
  3489.  
  3490.  
  3491.  
  3492.  
  3493.  
  3494.  
  3495.  
  3496.  
  3497.  
  3498.  
  3499.  
  3500.  
  3501.  
  3502.  
  3503.  
  3504.  
  3505.  
  3506.  
  3507.  
  3508.  
  3509.  
  3510.  
  3511.  
  3512.  
  3513.  
  3514.  
  3515.  
  3516.  
  3517.  
  3518.  
  3519.  
  3520.  
  3521.  
  3522.  
  3523.  
  3524.  
  3525.  
  3526.  
  3527.  
  3528.  
  3529.  
  3530.  
  3531.  
  3532.  
  3533.  
  3534.  
  3535.  
  3536.  
  3537. Jul 27 17:12 1984  6.17struct2.c Page 1
  3538.  
  3539.  
  3540. /* demonstration of a structure, 2nd version */
  3541.  
  3542. #define FSZ 20
  3543. #define LSZ 26
  3544. #include <stdio.h>
  3545. #include "getlinepn.f"  /* does not retain newline */
  3546. #include "getdbl.f"
  3547. struct
  3548. { char first[FSZ];
  3549.   char  last[LSZ];
  3550.   double     sal;
  3551. } emp;  /* external definition and declaration */
  3552. main()  /* populate then print structure */
  3553. { double getdbl();
  3554.   printf("input first name: "); getlinepn(emp.first, FSZ);
  3555.   printf("input last name: ");  getlinepn(emp.last, LSZ);
  3556.   printf("(annual salary) ");   emp.sal = getdbl();
  3557.   printf("%s %s earns $%.2f per year\n",
  3558.                 emp.first, emp.last, emp.sal);
  3559. }
  3560.  
  3561.  
  3562.  
  3563.  
  3564.  
  3565.  
  3566.  
  3567.  
  3568.  
  3569.  
  3570.  
  3571.  
  3572.  
  3573.  
  3574.  
  3575.  
  3576.  
  3577.  
  3578.  
  3579.  
  3580.  
  3581.  
  3582.  
  3583.  
  3584.  
  3585.  
  3586.  
  3587.  
  3588.  
  3589.  
  3590.  
  3591.  
  3592.  
  3593.  
  3594.  
  3595.  
  3596.  
  3597.  
  3598.  
  3599.  
  3600.  
  3601.  
  3602.  
  3603. Jul 27 17:12 1984  6.20aos.c Page 1
  3604.  
  3605.  
  3606. /* demonstration of an array of structures */
  3607.  
  3608. struct data
  3609. { char first[20];
  3610.   char  last[26];
  3611.   double     sal;
  3612. };              /* external definition */
  3613. main()          /* find and print last names M - Z */
  3614. { register i;
  3615.   static struct data ar[] = { { "Linda", "Sample", 39000.0 },
  3616.                               { "Sam", "Next", 37500.0 },
  3617.                               { "Larry", "Last", 34900.0 },
  3618.                              }; /* declare and init array ar */
  3619.   printf("employees with last names M - Z...\n");
  3620.   for (i = 0; i < (sizeof(ar) / sizeof(ar[0])); ++i)
  3621.     if (ar[i].last[0] >= 'M' && ar[i].last[0] <= 'Z')
  3622.         printf("\t%s %s earns $%.2f per year\n",
  3623.           ar[i].first, ar[i].last, ar[i].sal);
  3624. }
  3625.  
  3626.  
  3627.  
  3628.  
  3629.  
  3630.  
  3631.  
  3632.  
  3633.  
  3634.  
  3635.  
  3636.  
  3637.  
  3638.  
  3639.  
  3640.  
  3641.  
  3642.  
  3643.  
  3644.  
  3645.  
  3646.  
  3647.  
  3648.  
  3649.  
  3650.  
  3651.  
  3652.  
  3653.  
  3654.  
  3655.  
  3656.  
  3657.  
  3658.  
  3659.  
  3660.  
  3661.  
  3662.  
  3663.  
  3664.  
  3665.  
  3666.  
  3667.  
  3668.  
  3669. Jul 27 17:12 1984  6.24cbv.c Page 1
  3670.  
  3671.  
  3672. /* passing structures as arguments - by value */
  3673.  
  3674. struct data
  3675. { char first[20];
  3676.   char  last[26];
  3677.   double     sal;
  3678. };              /* external definition */
  3679. main()          /* test check() */
  3680. { register i;
  3681.   double pay = 36000.0;
  3682.   static struct data ar[] = { { "Linda", "Sample", 39000.0 },
  3683.                               { "Sam", "Next", 37500.0 },
  3684.                               { "Larry", "Last", 34900.0 },
  3685.                              }; /* declare and init array ar */
  3686.   printf("employees earning more than $%.2f per year...\n", pay);
  3687.   for (i = 0; i < (sizeof(ar) / sizeof(ar[0])); ++i)
  3688.     if (check(ar[i], pay))
  3689.         printf("\t%s %s earns $%.2f per year\n",
  3690.           ar[i].first, ar[i].last, ar[i].sal);
  3691. }
  3692. check(str, lim)         /* return 1 if sal >= lim */
  3693. struct data str;        /* struct declaration */
  3694. double lim;
  3695. { if (str.sal >= lim)
  3696.         return (1);
  3697.   return (0);
  3698. }
  3699.  
  3700.  
  3701.  
  3702.  
  3703.  
  3704.  
  3705.  
  3706.  
  3707.  
  3708.  
  3709.  
  3710.  
  3711.  
  3712.  
  3713.  
  3714.  
  3715.  
  3716.  
  3717.  
  3718.  
  3719.  
  3720.  
  3721.  
  3722.  
  3723.  
  3724.  
  3725.  
  3726.  
  3727.  
  3728.  
  3729.  
  3730.  
  3731.  
  3732.  
  3733.  
  3734.  
  3735. Jul 27 17:12 1984  6.26cbr.c Page 1
  3736.  
  3737.  
  3738. /* passing structures as arguments - by reference */
  3739.  
  3740. struct data
  3741. { char first[20];
  3742.   char  last[26];
  3743.   double     sal;
  3744. };              /* external definition */
  3745. main()          /* test check_p() */
  3746. { register i;
  3747.   double pay = 36000.0;
  3748.   static struct data ar[] = { { "Linda", "Sample", 39000.0 },
  3749.                               { "Sam", "Next", 37500.0 },
  3750.                               { "Larry", "Last", 34900.0 },
  3751.                              }; /* declare and init array ar */
  3752.   printf("employees earning more than $%.2f per year...\n", pay);
  3753.   for (i = 0; i < (sizeof(ar) / sizeof(ar[0])); ++i)
  3754.     if (check_p(&ar[i], pay))
  3755.         printf("\t%s %s earns $%.2f per year\n",
  3756.           ar[i].first, ar[i].last, ar[i].sal);
  3757. }
  3758. check_p(pstr, lim)      /* return 1 if sal >= lim */
  3759. struct data *pstr;      /* pointer to struct declaration */
  3760. double lim;
  3761. { if (pstr->sal >= lim)
  3762.         return (1);
  3763.   return (0);
  3764. }
  3765.  
  3766.  
  3767.  
  3768.  
  3769.  
  3770.  
  3771.  
  3772.  
  3773.  
  3774.  
  3775.  
  3776.  
  3777.  
  3778.  
  3779.  
  3780.  
  3781.  
  3782.  
  3783.  
  3784.  
  3785.  
  3786.  
  3787.  
  3788.  
  3789.  
  3790.  
  3791.  
  3792.  
  3793.  
  3794.  
  3795.  
  3796.  
  3797.  
  3798.  
  3799.  
  3800.  
  3801. Jul 27 17:12 1984  6.34fields.c Page 1
  3802.  
  3803.  
  3804. /* structure with fields */
  3805.  
  3806. #define YES 1
  3807. #define NO  0
  3808. struct
  3809. { char first[20];
  3810.   char  last[26];
  3811.   double     sal;
  3812.   unsigned retire : 1;  /* field width 1 bit */
  3813.   unsigned school : 1;  /* field width 1 bit */
  3814. } ar[] = { { "Linda", "Sample", 39000.0, NO,  NO },
  3815.            { "Sam",   "Next",   37500.0, NO, YES },
  3816.            { "Larry", "Last",   34900.0, YES, NO },
  3817.          };     /* declare and init external array */
  3818. main()  /* find and print active employees;
  3819.         /* note if in school */
  3820. { register i;
  3821.   printf("active employees...\n");
  3822.   for (i = 0; i < (sizeof(ar) / sizeof(ar[0])); ++i)
  3823.   { if (!ar[i].retire)
  3824.     {   printf("\t%s %s earns $%.2f per year\n",
  3825.            ar[i].first, ar[i].last, ar[i].sal);
  3826.         if (ar[i].school)
  3827.            printf("\t\t(in school)\n");
  3828.     }
  3829.   }
  3830. }
  3831.  
  3832.  
  3833.  
  3834.  
  3835.  
  3836.  
  3837.  
  3838.  
  3839.  
  3840.  
  3841.  
  3842.  
  3843.  
  3844.  
  3845.  
  3846.  
  3847.  
  3848.  
  3849.  
  3850.  
  3851.  
  3852.  
  3853.  
  3854.  
  3855.  
  3856.  
  3857.  
  3858.  
  3859.  
  3860.  
  3861.  
  3862.  
  3863.  
  3864.  
  3865.  
  3866.  
  3867. Aug  7 15:11 1984  6.40unions.c Page 1
  3868.  
  3869.  
  3870. union utag
  3871. { char  ca[256];        /* type 1 */
  3872.   int       num;        /* type 2 */
  3873. };                      /* external definition */
  3874. #include <stdio.h>
  3875. #include "getline.f"
  3876. #include "getint.f"
  3877. main(argc, argv)        /* load union, call chk_u() */
  3878. int argc;               /*      to test and print  */
  3879. char **argv;
  3880. { union utag udemo;     /* union declaration */
  3881.   ++argv;               /* point to argv[1]  */
  3882.   if (*++*argv != '\0') /* test 2nd char of *argv[1] */
  3883.     chk_u(0, &udemo);   /*      if not '\0' fail    */
  3884.   switch (*--*argv)     /* test 1st char of *argv[1] */
  3885.   { case '1': printf("input line: "); getline(udemo.ca, 256);
  3886.               chk_u(**argv, &udemo);
  3887.     case '2': udemo.num = getint(); chk_u(**argv, &udemo);
  3888.     default:  chk_u(**argv, &udemo);
  3889.   }
  3890. }
  3891. chk_u(utype, pun)       /* test, then print union and exit */
  3892. char utype;
  3893. union utag *pun;        /* pun is pointer to union */
  3894. if(pun -> ca == pun ->num)
  3895.      printf("same size");
  3896. else printf("different size");
  3897. { switch(utype)
  3898.   { case '1': printf("type 1, content...\n\t%s", pun->ca);
  3899.               break;
  3900.     case '2': printf("type 2, value is %d\n", pun->num);
  3901.               break;
  3902.     default:  printf("usage: filename { 1 2 }\n");
  3903.               exit(1);  /* exit unsuccessfully */
  3904.   }
  3905.   exit(0);              /* exit successfully */
  3906. }
  3907.  
  3908.  
  3909.  
  3910.  
  3911.  
  3912.  
  3913.  
  3914.  
  3915.  
  3916.  
  3917.  
  3918.  
  3919.  
  3920.  
  3921.  
  3922.  
  3923.  
  3924.  
  3925.  
  3926.  
  3927.  
  3928.  
  3929.  
  3930.  
  3931. pr 7.*
  3932.  
  3933.  
  3934. Jul 27 17:12 1984  7.14getdbl_n.c Page 1
  3935.  
  3936.  
  3937. #include <stdio.h>
  3938. main()                  /* test getdbl_n() */
  3939. {       double getdbl_n();
  3940.         printf("number is %f\n", getdbl_n());
  3941. }
  3942. double  getdbl_n()      /* convert ASCII to dbl - 2nd version */
  3943. {       int     c, sign = 1;    /* c = char input, sign = + */
  3944.         float   d = 1.0;        /* d = decimal place */
  3945.         double  num = 0.0;      /* num = converted number */
  3946.         printf("input number: ");
  3947.         if      ((c = getc(stdin)) == '-')
  3948.                 sign = -1;              /* sign = - */
  3949.         else if (c == '\n' || c == EOF)
  3950.                 return (0);
  3951.         else if (c == '.' || (c >= '0' && c <= '9'))
  3952.                 ungetc(c, stdin);       /* return c to buffer */
  3953.         while ((c = getc(stdin)) != '\n' && c != '.' && c != EOF)
  3954.                 if (c >= '0' && c <= '9')
  3955.                         num = num * 10 + (c - '0');
  3956.         if (c == '.')
  3957.                 while ((c = getc(stdin)) != EOF && c != '\n')
  3958.                         if (c >= '0' && c <= '9')
  3959.                         { d /= 10.0; num += d * (c - '0');
  3960.                         }
  3961.         return (num * sign);
  3962. }
  3963.  
  3964.  
  3965.  
  3966.  
  3967.  
  3968.  
  3969.  
  3970.  
  3971.  
  3972.  
  3973.  
  3974.  
  3975.  
  3976.  
  3977.  
  3978.  
  3979.  
  3980.  
  3981.  
  3982.  
  3983.  
  3984.  
  3985.  
  3986.  
  3987.  
  3988.  
  3989.  
  3990.  
  3991.  
  3992.  
  3993.  
  3994.  
  3995.  
  3996.  
  3997.  
  3998.  
  3999.  
  4000. Jul 27 17:12 1984  7.20cp.c Page 1
  4001.  
  4002.  
  4003. #include <stdio.h>
  4004. main(argc, argv)        /* modified cp(1) command */
  4005. int argc;
  4006. char *argv[];
  4007. { FILE *fpr, *fpw;      /* pointers to type FILE */
  4008.   if (argc != 3)
  4009.   { printf("usage: %s src_file dest_file\n", argv[0]);
  4010.     exit(1);
  4011.   }
  4012.   if ((fpr = fopen(argv[1], "r")) == NULL)
  4013.   { printf("%s: cannot access %s\n", argv[0], argv[1]);
  4014.     exit(2);
  4015.   }
  4016.   if ((fpw = fopen(argv[2], "w")) == NULL)
  4017.   { printf("%s: cannot create %s\n", argv[0], argv[2]);
  4018.     exit(2);
  4019.   }
  4020.   if (copy(fpr, fpw) == EOF)
  4021.         exit(0);        /* success */
  4022.   exit(3);              /* failure */
  4023. }
  4024. copy(fp1, fp2)          /* copy fp1 to fp2 */
  4025. FILE *fp1, *fp2;
  4026. {
  4027.         int c;
  4028.  
  4029.         while ((c = getc(fp1)) != EOF)
  4030.                 putc(c, fp2);
  4031.         return (c);
  4032. }
  4033.  
  4034.  
  4035.  
  4036.  
  4037.  
  4038.  
  4039.  
  4040.  
  4041.  
  4042.  
  4043.  
  4044.  
  4045.  
  4046.  
  4047.  
  4048.  
  4049.  
  4050.  
  4051.  
  4052.  
  4053.  
  4054.  
  4055.  
  4056.  
  4057.  
  4058.  
  4059.  
  4060.  
  4061.  
  4062.  
  4063.  
  4064.  
  4065.  
  4066. Jul 27 17:12 1984  7.27cat.c Page 1
  4067.  
  4068.  
  4069. #include <stdio.h>
  4070. main(argc, argv)        /* modified cat(1) command */
  4071. int argc;
  4072. char *argv[];
  4073. {
  4074.   register i, c;
  4075.   FILE *fp;             /* fp is pointer to type FILE */
  4076.  
  4077.   for (i = 1; i < argc; ++i)
  4078.   { if ((fp = fopen(argv[i], "r")) == NULL)
  4079.     {   fprintf(stderr, "%s: cannot open %s\n",
  4080.                 argv[0], argv[i]);
  4081.         continue;
  4082.     }
  4083.     while ((c = getc(fp)) != EOF)
  4084.         putc(c, stdout);
  4085.     fclose(fp);
  4086.   }
  4087. }
  4088.  
  4089.  
  4090.  
  4091.  
  4092.  
  4093.  
  4094.  
  4095.  
  4096.  
  4097.  
  4098.  
  4099.  
  4100.  
  4101.  
  4102.  
  4103.  
  4104.  
  4105.  
  4106.  
  4107.  
  4108.  
  4109.  
  4110.  
  4111.  
  4112.  
  4113.  
  4114.  
  4115.  
  4116.  
  4117.  
  4118.  
  4119.  
  4120.  
  4121.  
  4122.  
  4123.  
  4124.  
  4125.  
  4126.  
  4127.  
  4128.  
  4129.  
  4130.  
  4131.  
  4132. Jul 27 17:12 1984  7.34sscanf.c Page 1
  4133.  
  4134.  
  4135. #include <stdio.h>
  4136. char tst[] = "123456.78 592 89000 now is the time";
  4137. main()  /* demonstration of sscanf() */
  4138. {
  4139.   int   dec;
  4140.   float f;
  4141.   long  lg;
  4142.   char  s1[50], s2[50];
  4143.   int ret;
  4144.  
  4145.   ret = sscanf(tst, "%3d %f %*d %ld %s %[a-z ]",
  4146.                 &dec, &f, &lg, s1, s2);
  4147.   printf("return from sscanf() was %d\n", ret);
  4148.   printf("int dec is %d\n", dec);
  4149.   printf("float f is %.2f\n", f);
  4150.   printf("long lg is %ld\n", lg);
  4151.   printf("array s1 is \"%s\"\n", s1);
  4152.   printf("array s2 is \"%s\"\n", s2);
  4153. }
  4154.  
  4155.  
  4156.  
  4157.  
  4158.  
  4159.  
  4160.  
  4161.  
  4162.  
  4163.  
  4164.  
  4165.  
  4166.  
  4167.  
  4168.  
  4169.  
  4170.  
  4171.  
  4172.  
  4173.  
  4174.  
  4175.  
  4176.  
  4177.  
  4178.  
  4179.  
  4180.  
  4181.  
  4182.  
  4183.  
  4184.  
  4185.  
  4186.  
  4187.  
  4188.  
  4189.  
  4190.  
  4191.  
  4192.  
  4193.  
  4194.  
  4195.  
  4196.  
  4197.  
  4198. Jul 27 17:12 1984  7.46atof.c Page 1
  4199.  
  4200.  
  4201. /* another version of getdbl() */
  4202.  
  4203. #include <stdio.h>
  4204. #define MAX 256
  4205. main()  /* test getdbl_a() */
  4206. {       double getdbl_a();
  4207.  
  4208.         printf("num is %f\n", getdbl_a());
  4209. }
  4210. double getdbl_a()       /* use (3S) & (3C) functions */
  4211. {       char stor[MAX];
  4212.         double atof();
  4213.  
  4214.         printf("input number: ");
  4215.         return (atof(fgets(stor, MAX, stdin)));
  4216. }
  4217.  
  4218.  
  4219.  
  4220.  
  4221.  
  4222.  
  4223.  
  4224.  
  4225.  
  4226.  
  4227.  
  4228.  
  4229.  
  4230.  
  4231.  
  4232.  
  4233.  
  4234.  
  4235.  
  4236.  
  4237.  
  4238.  
  4239.  
  4240.  
  4241.  
  4242.  
  4243.  
  4244.  
  4245.  
  4246.  
  4247.  
  4248.  
  4249.  
  4250.  
  4251.  
  4252.  
  4253.  
  4254.  
  4255.  
  4256.  
  4257.  
  4258.  
  4259.  
  4260.  
  4261.  
  4262.  
  4263.  
  4264. Jul 27 17:12 1984  7.50create.c Page 1
  4265.  
  4266.  
  4267. #include <stdio.h>
  4268. #define SCORE 8
  4269. struct record
  4270. {       char name[48];
  4271.         float score[SCORE];
  4272. };
  4273. main()  /* create DATAFILE with student names and scores */
  4274. {       struct record data;
  4275.         FILE *fp;
  4276.         if ((fp = fopen("DATAFILE", "w")) == NULL)
  4277.         {       fprintf(stderr, "cannot create DATAFILE\n");
  4278.                 exit(2);
  4279.         }
  4280.         while (load(&data))
  4281.                 fwrite(&data, sizeof (data), 1, fp);
  4282.         exit(0);
  4283. }
  4284. load(pstr)      /* get data from terminal; load structure */
  4285. struct record *pstr;
  4286. {
  4287.         register i;
  4288.         printf("\ninput name: ");
  4289.         gets(pstr->name);
  4290.         if (pstr->name[0] == '\0')
  4291.                 return (0);
  4292.         printf("input up to %d scores ('q' to quit): ", SCORE);
  4293.         for (i = 0; i < SCORE ; ++i)
  4294.                 if (scanf("%f", &pstr->score[i]) != 1)
  4295.                 {       pstr->score[i] = EOF;
  4296.                         break;
  4297.                 }
  4298.         while (getchar() != '\n')       /* buffer drain */
  4299.                 ;
  4300.         return (1);
  4301. }
  4302.  
  4303.  
  4304.  
  4305.  
  4306.  
  4307.  
  4308.  
  4309.  
  4310.  
  4311.  
  4312.  
  4313.  
  4314.  
  4315.  
  4316.  
  4317.  
  4318.  
  4319.  
  4320.  
  4321.  
  4322.  
  4323.  
  4324.  
  4325.  
  4326.  
  4327.  
  4328.  
  4329.  
  4330. Jul 27 17:12 1984  7.52getinfo.c Page 1
  4331.  
  4332.  
  4333. #include <stdio.h>
  4334. #define SCORE 8
  4335. struct record
  4336. {       char name[48];
  4337.         float score[SCORE];
  4338. };
  4339. main()  /* get information from DATAFILE */
  4340. {       struct record data;
  4341.         FILE *fp;
  4342.         if ((fp = fopen("DATAFILE", "r")) == NULL)
  4343.         {       fprintf(stderr, "cannot open DATAFILE\n");
  4344.                 exit(2);
  4345.         }
  4346.         while (fread(&data, sizeof (data), 1, fp))
  4347.                 print(&data);
  4348.         exit(0);
  4349. }
  4350. print(pstr)     /* compute student ave; provide printout */
  4351. struct record *pstr;
  4352. {
  4353.         register i, cnt = 0;
  4354.         double num = 0.0;
  4355.         for (i = 0; i < SCORE ; ++i)
  4356.                 if      (pstr->score[i] != EOF)
  4357.                 {       printf("%7.2f", pstr->score[i]);
  4358.                         num += pstr->score[i];
  4359.                         ++cnt;
  4360.                 }
  4361.                 else    for (   ; i < SCORE; ++i)
  4362.                                 printf("   --- ");
  4363.         if      (cnt > 0)
  4364.                 printf(" (%6.2f)", num / (double) cnt);
  4365.         else    printf(" (  --- )");
  4366.         printf(" %s\n", pstr->name);
  4367. }
  4368.  
  4369.  
  4370.  
  4371.  
  4372.  
  4373.  
  4374.  
  4375.  
  4376.  
  4377.  
  4378.  
  4379.  
  4380.  
  4381.  
  4382.  
  4383.  
  4384.  
  4385.  
  4386.  
  4387.  
  4388.  
  4389.  
  4390.  
  4391.  
  4392.  
  4393.  
  4394.  
  4395.  
  4396. Jul 27 17:12 1984  7.56checknum.c Page 1
  4397.  
  4398.  
  4399. #include <stdio.h>
  4400. #define SCORE 8
  4401. struct record
  4402. {       char name[48];
  4403.         float score[SCORE];
  4404. };
  4405. main()  /* check number of entries in DATAFILE */
  4406. {       struct record data;
  4407.         FILE *fp;
  4408.         if ((fp = fopen("DATAFILE", "r")) == NULL)
  4409.         {       fprintf(stderr, "cannot open DATAFILE\n");
  4410.                 exit(2);
  4411.         }
  4412.         fseek(fp, 0L, 2);       /* move pointer to end-of-file */
  4413.         printf("%5ld entries in DATAFILE\n", ftell(fp) / (long) sizeof (data));
  4414.         exit(0);
  4415. }
  4416.  
  4417.  
  4418.  
  4419.  
  4420.  
  4421.  
  4422.  
  4423.  
  4424.  
  4425.  
  4426.  
  4427.  
  4428.  
  4429.  
  4430.  
  4431.  
  4432.  
  4433.  
  4434.  
  4435.  
  4436.  
  4437.  
  4438.  
  4439.  
  4440.  
  4441.  
  4442.  
  4443.  
  4444.  
  4445.  
  4446.  
  4447.  
  4448.  
  4449.  
  4450.  
  4451.  
  4452.  
  4453.  
  4454.  
  4455.  
  4456.  
  4457.  
  4458.  
  4459.  
  4460.  
  4461.  
  4462. Jul 27 17:12 1984  7.59system.c Page 1
  4463.  
  4464.  
  4465. main()  /* use sh(1) to get
  4466.         /* long listing of DATAFILE
  4467.         /* and use same exit status */
  4468. {
  4469.         unsigned ret;   /* machine independent */
  4470.  
  4471.         ret = system("ls -l DATAFILE");
  4472.         exit(ret >> 8); /* shift right 8 places
  4473.                         /* to get sh(1) exit status */
  4474. }
  4475.  
  4476.  
  4477.  
  4478.  
  4479.  
  4480.  
  4481.  
  4482.  
  4483.  
  4484.  
  4485.  
  4486.  
  4487.  
  4488.  
  4489.  
  4490.  
  4491.  
  4492.  
  4493.  
  4494.  
  4495.  
  4496.  
  4497.  
  4498.  
  4499.  
  4500.  
  4501.  
  4502.  
  4503.  
  4504.  
  4505.  
  4506.  
  4507.  
  4508.  
  4509.  
  4510.  
  4511.  
  4512.  
  4513.  
  4514.  
  4515.  
  4516.  
  4517.  
  4518.  
  4519.  
  4520.  
  4521.  
  4522.  
  4523.  
  4524.  
  4525.  
  4526.  
  4527.  
  4528. Jul 27 17:12 1984  7.70cp2.c Page 1
  4529.  
  4530.  
  4531. #include <fcntl.h>      /* included for open(2) */
  4532. main(argc, argv)        /* modified cp(1) command */
  4533. int argc;               /* 2nd version */
  4534. char *argv[];           /* using system calls */
  4535. { int fdr, fdw;         /* file descriptors */
  4536.   if (argc != 3)
  4537.   { printf("usage: %s src_file dest_file\n", argv[0]);
  4538.     exit(1);
  4539.   }
  4540.   if ((fdr = open(argv[1], 0)) == -1)
  4541.   { printf("%s: cannot access %s\n", argv[0], argv[1]);
  4542.     exit(2);
  4543.   }
  4544.   if ((fdw = creat(argv[2], 0644)) == -1)
  4545.   { printf("%s: cannot create %s\n", argv[0], argv[2]);
  4546.     exit(2);
  4547.   }
  4548.   if (copy(fdr, fdw) == 0)
  4549.         exit(0);        /* success */
  4550.   exit(3);              /* failure */
  4551. }
  4552. copy(fd1, fd2)          /* copy fd1 to fd2 */
  4553. int fd1, fd2;
  4554. { register n;
  4555.   char buf[512];        /* temp character storage */
  4556.  
  4557.   while ((n = read(fd1, buf, 512)) > 0)
  4558.     write(fd2, buf, n);
  4559.   return (n);
  4560. }
  4561.  
  4562.  
  4563.  
  4564.  
  4565.  
  4566.  
  4567.  
  4568.  
  4569.  
  4570.  
  4571.  
  4572.  
  4573.  
  4574.  
  4575.  
  4576.  
  4577.  
  4578.  
  4579.  
  4580.  
  4581.  
  4582.  
  4583.  
  4584.  
  4585.  
  4586.  
  4587.  
  4588.  
  4589.  
  4590.  
  4591.  
  4592. pr *.f
  4593.  
  4594.  
  4595. Jul 27 17:12 1984  fact.f Page 1
  4596.  
  4597.  
  4598. double fact(num)        /* recursive function to compute and
  4599.                            return factorial (num!) for num > 0 */
  4600. int num;
  4601. {       if      (num > 1)
  4602.                 return (num * fact(num - 1));
  4603.         else    return 1;
  4604. }
  4605.  
  4606.  
  4607.  
  4608.  
  4609.  
  4610.  
  4611.  
  4612.  
  4613.  
  4614.  
  4615.  
  4616.  
  4617.  
  4618.  
  4619.  
  4620.  
  4621.  
  4622.  
  4623.  
  4624.  
  4625.  
  4626.  
  4627.  
  4628.  
  4629.  
  4630.  
  4631.  
  4632.  
  4633.  
  4634.  
  4635.  
  4636.  
  4637.  
  4638.  
  4639.  
  4640.  
  4641.  
  4642.  
  4643.  
  4644.  
  4645.  
  4646.  
  4647.  
  4648.  
  4649.  
  4650.  
  4651.  
  4652.  
  4653.  
  4654.  
  4655.  
  4656.  
  4657.  
  4658.  
  4659.  
  4660.  
  4661. Jul 27 17:12 1984  getdbl.f Page 1
  4662.  
  4663.  
  4664. double  getdbl()        /* convert ASCII from terminal to dbl */
  4665. {       int     c, sign = 1;    /* c = char input, sign = + */
  4666.         float   d = 1.0;        /* d = decimal place */
  4667.         double  num = 0.0;      /* num = converted number */
  4668.         printf("input number: ");
  4669.         if      ((c = getchar()) == '-')
  4670.                 sign = -1;                      /* sign = - */
  4671.         else if (c == '\n' || c == EOF)
  4672.                 return (0);
  4673.         else if (c == '.')
  4674.                 goto dot;
  4675.         else if (c >= '0' && c <= '9')
  4676.                 num = (c - '0');
  4677.         while ((c = getchar()) != '\n' && c != '.' && c != EOF)
  4678.                 if (c >= '0' && c <= '9')
  4679.                         num = num * 10 + (c - '0');
  4680.         if (c == '.')
  4681. dot:            while ((c = getchar()) != EOF && c != '\n')
  4682.                         if (c >= '0' && c <= '9')
  4683.                         { d /= 10.0; num += d * (c - '0');
  4684.                         }
  4685.         return (num * sign);
  4686. }
  4687.  
  4688.  
  4689.  
  4690.  
  4691.  
  4692.  
  4693.  
  4694.  
  4695.  
  4696.  
  4697.  
  4698.  
  4699.  
  4700.  
  4701.  
  4702.  
  4703.  
  4704.  
  4705.  
  4706.  
  4707.  
  4708.  
  4709.  
  4710.  
  4711.  
  4712.  
  4713.  
  4714.  
  4715.  
  4716.  
  4717.  
  4718.  
  4719.  
  4720.  
  4721.  
  4722.  
  4723.  
  4724.  
  4725.  
  4726.  
  4727. Jul 27 17:12 1984  getdbl_a.f Page 1
  4728.  
  4729.  
  4730. double getdbl_a()       /* use (3S) & (3C) functions */
  4731. {       char stor[MAX];
  4732.         double atof();
  4733.  
  4734.         printf("input number: ");
  4735.         return (atof(fgets(stor, MAX, stdin)));
  4736. }
  4737.  
  4738.  
  4739.  
  4740.  
  4741.  
  4742.  
  4743.  
  4744.  
  4745.  
  4746.  
  4747.  
  4748.  
  4749.  
  4750.  
  4751.  
  4752.  
  4753.  
  4754.  
  4755.  
  4756.  
  4757.  
  4758.  
  4759.  
  4760.  
  4761.  
  4762.  
  4763.  
  4764.  
  4765.  
  4766.  
  4767.  
  4768.  
  4769.  
  4770.  
  4771.  
  4772.  
  4773.  
  4774.  
  4775.  
  4776.  
  4777.  
  4778.  
  4779.  
  4780.  
  4781.  
  4782.  
  4783.  
  4784.  
  4785.  
  4786.  
  4787.  
  4788.  
  4789.  
  4790.  
  4791.  
  4792.  
  4793. Jul 27 17:12 1984  getdbl_n.f Page 1
  4794.  
  4795.  
  4796. double  getdbl_n()      /* convert ASCII to dbl - 2nd version */
  4797. {       int     c, sign = 1;    /* c = char input, sign = + */
  4798.         float   d = 1.0;        /* d = decimal place */
  4799.         double  num = 0.0;      /* num = converted number */
  4800.         printf("input number: ");
  4801.         if      ((c = getc(stdin)) == '-')
  4802.                 sign = -1;              /* sign = - */
  4803.         else if (c == '\n' || c == EOF)
  4804.                 return (0);
  4805.         else if (c == '.' || (c >= '0' && c <= '9'))
  4806.                 ungetc(c, stdin);       /* return c to buffer */
  4807.         while ((c = getc(stdin)) != '\n' && c != '.' && c != EOF)
  4808.                 if (c >= '0' && c <= '9')
  4809.                         num = num * 10 + (c - '0');
  4810.         if (c == '.')
  4811.                 while ((c = getc(stdin)) != EOF && c != '\n')
  4812.                         if (c >= '0' && c <= '9')
  4813.                         { d /= 10.0; num += d * (c - '0');
  4814.                         }
  4815.         return (num * sign);
  4816. }
  4817.  
  4818.  
  4819.  
  4820.  
  4821.  
  4822.  
  4823.  
  4824.  
  4825.  
  4826.  
  4827.  
  4828.  
  4829.  
  4830.  
  4831.  
  4832.  
  4833.  
  4834.  
  4835.  
  4836.  
  4837.  
  4838.  
  4839.  
  4840.  
  4841.  
  4842.  
  4843.  
  4844.  
  4845.  
  4846.  
  4847.  
  4848.  
  4849.  
  4850.  
  4851.  
  4852.  
  4853.  
  4854.  
  4855.  
  4856.  
  4857.  
  4858.  
  4859. Jul 27 17:12 1984  getint.f Page 1
  4860.  
  4861.  
  4862. getint()        /* function to convert ASCII
  4863.                 /* input from terminal to an int */
  4864. {       int     c, sign = 1, num = 0;
  4865.  
  4866.         printf("input integer: ");
  4867.         if      ((c = getchar()) == '-')
  4868.                 sign = -1;
  4869.         else if (c >= '0' && c <= '9')
  4870.                 num = (c - '0');
  4871.         else
  4872.                 return (0);
  4873.         while ((c = getchar()) >= '0' && c <= '9')
  4874.                 num = num * 10 + (c - '0');
  4875.         return (num * sign);
  4876. }
  4877.  
  4878.  
  4879.  
  4880.  
  4881.  
  4882.  
  4883.  
  4884.  
  4885.  
  4886.  
  4887.  
  4888.  
  4889.  
  4890.  
  4891.  
  4892.  
  4893.  
  4894.  
  4895.  
  4896.  
  4897.  
  4898.  
  4899.  
  4900.  
  4901.  
  4902.  
  4903.  
  4904.  
  4905.  
  4906.  
  4907.  
  4908.  
  4909.  
  4910.  
  4911.  
  4912.  
  4913.  
  4914.  
  4915.  
  4916.  
  4917.  
  4918.  
  4919.  
  4920.  
  4921.  
  4922.  
  4923.  
  4924.  
  4925. Jul 27 17:12 1984  getintp.f Page 1
  4926.  
  4927.  
  4928. int *getintp()  /* function to convert ASCII
  4929.                 /* input from terminal to an int;
  4930.                 /* return pointer to int */
  4931. { int   c, sign = 1, num = 0;
  4932.  
  4933.   printf("input integer: ");
  4934.   if    ((c = getchar()) == '-')
  4935.         sign = -1;
  4936.   else if (c >= '0' && c <= '9')
  4937.         num = (c - '0');
  4938.   else
  4939.         return (&num);
  4940.   while ((c = getchar()) >= '0' && c <= '9')
  4941.         num = num * 10 + (c - '0');
  4942.   num *= sign;
  4943.   return (&num);
  4944. }
  4945.  
  4946.  
  4947.  
  4948.  
  4949.  
  4950.  
  4951.  
  4952.  
  4953.  
  4954.  
  4955.  
  4956.  
  4957.  
  4958.  
  4959.  
  4960.  
  4961.  
  4962.  
  4963.  
  4964.  
  4965.  
  4966.  
  4967.  
  4968.  
  4969.  
  4970.  
  4971.  
  4972.  
  4973.  
  4974.  
  4975.  
  4976.  
  4977.  
  4978.  
  4979.  
  4980.  
  4981.  
  4982.  
  4983.  
  4984.  
  4985.  
  4986.  
  4987.  
  4988.  
  4989.  
  4990.  
  4991. Jul 27 17:12 1984  getline.f Page 1
  4992.  
  4993.  
  4994. getline(line, lim)      /* function to read a line from std input;
  4995.                         /* insert NULL at end and return char count */
  4996. char    line[];         /* array where line will be stored */
  4997. int     lim;            /* max line size */
  4998. {
  4999.   int   c, i;
  5000.  
  5001.   for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
  5002.         line[i] = c;
  5003.   if (c == '\n')                /* retain  */
  5004.         line[i++] = c;          /* newline */
  5005.   line[i] = '\0'; return (i);
  5006. }
  5007.  
  5008.  
  5009.  
  5010.  
  5011.  
  5012.  
  5013.  
  5014.  
  5015.  
  5016.  
  5017.  
  5018.  
  5019.  
  5020.  
  5021.  
  5022.  
  5023.  
  5024.  
  5025.  
  5026.  
  5027.  
  5028.  
  5029.  
  5030.  
  5031.  
  5032.  
  5033.  
  5034.  
  5035.  
  5036.  
  5037.  
  5038.  
  5039.  
  5040.  
  5041.  
  5042.  
  5043.  
  5044.  
  5045.  
  5046.  
  5047.  
  5048.  
  5049.  
  5050.  
  5051.  
  5052.  
  5053.  
  5054.  
  5055.  
  5056.  
  5057. Aug  3 14:21 1984  getlinep.f Page 1
  5058.  
  5059.  
  5060. getlinep(line, lim)     /* function to read a line from std input;
  5061.                         /* insert NULL at end and return char count */
  5062. char    *line;          /* array where line will be stored */
  5063. int     lim;            /* max line size */
  5064. {
  5065.   int   c, i;
  5066.  
  5067.   for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
  5068.         *line++ = c;
  5069.   if (c == '\n')                /* retain  */
  5070.         *line++ = c;            /* newline */
  5071.   *line = '\0'; return (i);
  5072. }
  5073.  
  5074.  
  5075.  
  5076.  
  5077.  
  5078.  
  5079.  
  5080.  
  5081.  
  5082.  
  5083.  
  5084.  
  5085.  
  5086.  
  5087.  
  5088.  
  5089.  
  5090.  
  5091.  
  5092.  
  5093.  
  5094.  
  5095.  
  5096.  
  5097.  
  5098.  
  5099.  
  5100.  
  5101.  
  5102.  
  5103.  
  5104.  
  5105.  
  5106.  
  5107.  
  5108.  
  5109.  
  5110.  
  5111.  
  5112.  
  5113.  
  5114.  
  5115.  
  5116.  
  5117.  
  5118.  
  5119.  
  5120.  
  5121.  
  5122.  
  5123. Jul 27 17:12 1984  getlinepn.f Page 1
  5124.  
  5125.  
  5126. getlinepn(pline, lim)   /* function to get a line from std input
  5127.                         /* (do not save newline); insert null at end
  5128.                         /* and return char count per PA ex. 04a */
  5129. char    *pline;         /* pointer to array where line will be stored */
  5130. int     lim;            /* max line size */
  5131. {
  5132.   register c, i;
  5133.  
  5134.   for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
  5135.         *pline++ = c;           /* newline is                   */
  5136.   *pline = '\0'; return (i);    /*      not retained or counted */
  5137. }
  5138.  
  5139.  
  5140.  
  5141.  
  5142.  
  5143.  
  5144.  
  5145.  
  5146.  
  5147.  
  5148.  
  5149.  
  5150.  
  5151.  
  5152.  
  5153.  
  5154.  
  5155.  
  5156.  
  5157.  
  5158.  
  5159.  
  5160.  
  5161.  
  5162.  
  5163.  
  5164.  
  5165.  
  5166.  
  5167.  
  5168.  
  5169.  
  5170.  
  5171.  
  5172.  
  5173.  
  5174.  
  5175.  
  5176.  
  5177.  
  5178.  
  5179.  
  5180.  
  5181.  
  5182.  
  5183.  
  5184.  
  5185.  
  5186.  
  5187.  
  5188.  
  5189. Jul 27 17:12 1984  inter.f Page 1
  5190.  
  5191.  
  5192. inter(pfi)      /* call function via pointer arg */
  5193. int (*pfi)();   /* pfi is pointer to function
  5194.                 /* returning type int */
  5195. {
  5196.   return ((*pfi)());    /* (*pfi)() is a function call */
  5197. }
  5198.  
  5199.  
  5200.  
  5201.  
  5202.  
  5203.  
  5204.  
  5205.  
  5206.  
  5207.  
  5208.  
  5209.  
  5210.  
  5211.  
  5212.  
  5213.  
  5214.  
  5215.  
  5216.  
  5217.  
  5218.  
  5219.  
  5220.  
  5221.  
  5222.  
  5223.  
  5224.  
  5225.  
  5226.  
  5227.  
  5228.  
  5229.  
  5230.  
  5231.  
  5232.  
  5233.  
  5234.  
  5235.  
  5236.  
  5237.  
  5238.  
  5239.  
  5240.  
  5241.  
  5242.  
  5243.  
  5244.  
  5245.  
  5246.  
  5247.  
  5248.  
  5249.  
  5250.  
  5251.  
  5252.  
  5253.  
  5254.  
  5255. Jul 27 17:12 1984  itob.f Page 1
  5256.  
  5257.  
  5258. itob(num, ar)   /* convert int to ASCII 0's and 1's */
  5259. int num;        /* received int */
  5260. char ar[];      /* array to store 0's and 1's */
  5261. {       int cnt, mask = 1;
  5262.         for (cnt = (8 * sizeof(cnt)) - 1; cnt >= 0; cnt--)
  5263.         {       ar[cnt] = ((num & mask)? '1': '0'); mask <<= 1;
  5264.         }
  5265.         ar[(8 * sizeof(cnt))] = '\0';
  5266. }
  5267.  
  5268.  
  5269.  
  5270.  
  5271.  
  5272.  
  5273.  
  5274.  
  5275.  
  5276.  
  5277.  
  5278.  
  5279.  
  5280.  
  5281.  
  5282.  
  5283.  
  5284.  
  5285.  
  5286.  
  5287.  
  5288.  
  5289.  
  5290.  
  5291.  
  5292.  
  5293.  
  5294.  
  5295.  
  5296.  
  5297.  
  5298.  
  5299.  
  5300.  
  5301.  
  5302.  
  5303.  
  5304.  
  5305.  
  5306.  
  5307.  
  5308.  
  5309.  
  5310.  
  5311.  
  5312.  
  5313.  
  5314.  
  5315.  
  5316.  
  5317.  
  5318.  
  5319.  
  5320.  
  5321. Jul 27 17:12 1984  power.f Page 1
  5322.  
  5323.  
  5324. power(x, n)     /* raise x to the n-th power; n > 0 */
  5325. int x, n;       /* declaration of arguments */
  5326. {
  5327.         int i, p = 1;
  5328.         for (i = 1; i <= n; ++i)
  5329.                 p = p * x;
  5330.         return (p);
  5331. }
  5332.  
  5333.  
  5334.  
  5335.  
  5336.  
  5337.  
  5338.  
  5339.  
  5340.  
  5341.  
  5342.  
  5343.  
  5344.  
  5345.  
  5346.  
  5347.  
  5348.  
  5349.  
  5350.  
  5351.  
  5352.  
  5353.  
  5354.  
  5355.  
  5356.  
  5357.  
  5358.  
  5359.  
  5360.  
  5361.  
  5362.  
  5363.  
  5364.  
  5365.  
  5366.  
  5367.  
  5368.  
  5369.  
  5370.  
  5371.  
  5372.  
  5373.  
  5374.  
  5375.  
  5376.  
  5377.  
  5378.  
  5379.  
  5380.  
  5381.  
  5382.  
  5383.  
  5384.  
  5385.  
  5386.  
  5387. Jul 27 17:12 1984  square.f Page 1
  5388.  
  5389.  
  5390. square(ar, sz)  /* fills array ar of size sz with the
  5391.                 /* square of the element number */
  5392. int ar[], sz;
  5393. {
  5394.         for (--sz; sz >= 0; --sz)
  5395.                 ar[sz] = sz * sz;
  5396. }
  5397.  
  5398.  
  5399.  
  5400.  
  5401.  
  5402.  
  5403.  
  5404.  
  5405.  
  5406.  
  5407.  
  5408.  
  5409.  
  5410.  
  5411.  
  5412.  
  5413.  
  5414.  
  5415.  
  5416.  
  5417.  
  5418.  
  5419.  
  5420.  
  5421.  
  5422.  
  5423.  
  5424.  
  5425.  
  5426.  
  5427.  
  5428.  
  5429.  
  5430.  
  5431.  
  5432.  
  5433.  
  5434.  
  5435.  
  5436.  
  5437.  
  5438.  
  5439.  
  5440.  
  5441.  
  5442.  
  5443.  
  5444.  
  5445.  
  5446.  
  5447.  
  5448.  
  5449.  
  5450.  
  5451.  
  5452.  
  5453. Jul 27 17:12 1984  strcmp.f Page 1
  5454.  
  5455.  
  5456. strcmp(s1, s2)  /* compare 2 strings;
  5457.                 /* if match return 0 (else char count) */
  5458. char    *s1, *s2;
  5459. {       register i;
  5460.         for (i = 1; *s1 == *s2; ++i)
  5461.                 if (*s1++ == '\0')
  5462.                         return (0);
  5463.                 else s2++;
  5464.         return (i);
  5465. }
  5466.  
  5467.  
  5468.  
  5469.  
  5470.  
  5471.  
  5472.  
  5473.  
  5474.  
  5475.  
  5476.  
  5477.  
  5478.  
  5479.  
  5480.  
  5481.  
  5482.  
  5483.  
  5484.  
  5485.  
  5486.  
  5487.  
  5488.  
  5489.  
  5490.  
  5491.  
  5492.  
  5493.  
  5494.  
  5495.  
  5496.  
  5497.  
  5498.  
  5499.  
  5500.  
  5501.  
  5502.  
  5503.  
  5504.  
  5505.  
  5506.  
  5507.  
  5508.  
  5509.  
  5510.  
  5511.  
  5512.  
  5513.  
  5514.  
  5515.  
  5516.  
  5517.  
  5518.  
  5519. Jul 27 17:12 1984  strcpy.f Page 1
  5520.  
  5521.  
  5522. strcpy(d, s)    /* copy string in s[] into d[] */
  5523. char d[], s[];
  5524. {       register i;
  5525.         for (i = 0; d[i] = s[i]; ++i)
  5526.                 ;
  5527. }
  5528.  
  5529.  
  5530.  
  5531.  
  5532.  
  5533.  
  5534.  
  5535.  
  5536.  
  5537.  
  5538.  
  5539.  
  5540.  
  5541.  
  5542.  
  5543.  
  5544.  
  5545.  
  5546.  
  5547.  
  5548.  
  5549.  
  5550.  
  5551.  
  5552.  
  5553.  
  5554.  
  5555.  
  5556.  
  5557.  
  5558.  
  5559.  
  5560.  
  5561.  
  5562.  
  5563.  
  5564.  
  5565.  
  5566.  
  5567.  
  5568.  
  5569.  
  5570.  
  5571.  
  5572.  
  5573.  
  5574.  
  5575.  
  5576.  
  5577.  
  5578.  
  5579.  
  5580.  
  5581.  
  5582.  
  5583.  
  5584.  
  5585. Jul 27 17:12 1984  strcpy_p.f Page 1
  5586.  
  5587.  
  5588. strcpy_p(d, s)  /* copy string *s to *d */
  5589. char *d, *s;
  5590. {       
  5591.         while (*d++ = *s++)
  5592.                 ;
  5593. }
  5594.  
  5595.  
  5596.  
  5597.  
  5598.  
  5599.  
  5600.  
  5601.  
  5602.  
  5603.  
  5604.  
  5605.  
  5606.  
  5607.  
  5608.  
  5609.  
  5610.  
  5611.  
  5612.  
  5613.  
  5614.  
  5615.  
  5616.  
  5617.  
  5618.  
  5619.  
  5620.  
  5621.  
  5622.  
  5623.  
  5624.  
  5625.  
  5626.  
  5627.  
  5628.  
  5629.  
  5630.  
  5631.  
  5632.  
  5633.  
  5634.  
  5635.  
  5636.  
  5637.  
  5638.  
  5639.  
  5640.  
  5641.  
  5642.  
  5643.  
  5644.  
  5645.  
  5646.  
  5647.  
  5648.  
  5649.  
  5650.  
  5651. Jul 27 17:12 1984  strln.f Page 1
  5652.  
  5653.  
  5654. strln(str)      /* return length of char string stored in array str;
  5655.                 /* include null at end in count */
  5656. char str[];
  5657. {       int i = 0, length = 0;
  5658.         do
  5659.         {       ++length;
  5660.         } while (str[i++] != '\0');
  5661.         return (length);
  5662. }
  5663.  
  5664.  
  5665.  
  5666.  
  5667.  
  5668.  
  5669.  
  5670.  
  5671.  
  5672.  
  5673.  
  5674.  
  5675.  
  5676.  
  5677.  
  5678.  
  5679.  
  5680.  
  5681.  
  5682.  
  5683.  
  5684.  
  5685.  
  5686.  
  5687.  
  5688.  
  5689.  
  5690.  
  5691.  
  5692.  
  5693.  
  5694.  
  5695.  
  5696.  
  5697.  
  5698.  
  5699.  
  5700.  
  5701.  
  5702.  
  5703.  
  5704.  
  5705.  
  5706.  
  5707.  
  5708.  
  5709.  
  5710.  
  5711.  
  5712.  
  5713.  
  5714.  
  5715.  
  5716.  
  5717. Jul 27 17:12 1984  strln_p.f Page 1
  5718.  
  5719.  
  5720. strln_p(ptr)    /* return length of char string pointed to by ptr;
  5721.                 /* include null at end in count */
  5722. char *ptr;
  5723. {       register length = 0;
  5724.         do
  5725.         {       ++length;
  5726.         } while (*ptr++ != '\0');       /* ++ increments ptr by 1 */
  5727.         return (length);
  5728. }
  5729.  
  5730.  
  5731.  
  5732.  
  5733.  
  5734.  
  5735.  
  5736.  
  5737.  
  5738.  
  5739.  
  5740.  
  5741.  
  5742.  
  5743.  
  5744.  
  5745.  
  5746.  
  5747.  
  5748.  
  5749.  
  5750.  
  5751.  
  5752.  
  5753.  
  5754.  
  5755.  
  5756.  
  5757.  
  5758.  
  5759.  
  5760.  
  5761.  
  5762.  
  5763.  
  5764.  
  5765.  
  5766.  
  5767.  
  5768.  
  5769.  
  5770.  
  5771.  
  5772.  
  5773.  
  5774.  
  5775.  
  5776.  
  5777.  
  5778.  
  5779.  
  5780.  
  5781.  
  5782.  
  5783. Jul 27 17:12 1984  swap.f Page 1
  5784.  
  5785.  
  5786. swap(px, py)            /* call by reference */
  5787. int *px, *py;           /* px, py are pointers to type int */
  5788. {       int tmp;
  5789.         tmp = *px;
  5790.         *px = *py;
  5791.         *py = tmp;
  5792. }
  5793.  
  5794.  
  5795.  
  5796.  
  5797.  
  5798.  
  5799.  
  5800.  
  5801.  
  5802.  
  5803.  
  5804.  
  5805.  
  5806.  
  5807.  
  5808.  
  5809.  
  5810.  
  5811.  
  5812.  
  5813.  
  5814.  
  5815.  
  5816.  
  5817.  
  5818.  
  5819.  
  5820.  
  5821.  
  5822.  
  5823.  
  5824.  
  5825.  
  5826.  
  5827.  
  5828.  
  5829.  
  5830.  
  5831.  
  5832.  
  5833.  
  5834.  
  5835.  
  5836.  
  5837.  
  5838.  
  5839.  
  5840.  
  5841.  
  5842.  
  5843.  
  5844.  
  5845.  
  5846.  
  5847.  
  5848.  
  5849. Jul 27 17:12 1984  vol.f Page 1
  5850.  
  5851.  
  5852. double vol(a, b, c)     /* given 3 dimensions compute volume */
  5853. double a, b, c;         /* arg declarations */
  5854. {
  5855.         return (a * b * c);
  5856. }
  5857.  
  5858.  
  5859.  
  5860.  
  5861.  
  5862.  
  5863.  
  5864.  
  5865.  
  5866.  
  5867.  
  5868.  
  5869.  
  5870.  
  5871.  
  5872.  
  5873.  
  5874.  
  5875.  
  5876.  
  5877.  
  5878.  
  5879.  
  5880.  
  5881.  
  5882.  
  5883.  
  5884.  
  5885.  
  5886.  
  5887.  
  5888.  
  5889.  
  5890.  
  5891.  
  5892.  
  5893.  
  5894.  
  5895.  
  5896.  
  5897.  
  5898.  
  5899.  
  5900.  
  5901.  
  5902.  
  5903.  
  5904.  
  5905.  
  5906.  
  5907.  
  5908.  
  5909.  
  5910.  
  5911.  
  5912.  
  5913. ls -la
  5914. total 251
  5915. drwxr-xr-x   2 cp18     cp          2592 Aug 10 00:18 .
  5916. drwxr-xr-x  55 root     sys          928 Aug  6 18:19 ..
  5917. -rwxr-xr-x   1 cp18     cp           205 Jul 31 15:00 .profile
  5918. -rw-r--r--   1 cp18     cp           330 Jul 31 13:15 1.00array.c
  5919. -rw-r--r--   1 cp18     cp           144 Jul 27 17:12 1.10first.c
  5920. -rw-r--r--   1 cp18     cp           266 Jul 27 17:12 1.29while.c
  5921. -rw-r--r--   1 cp18     cp           195 Jul 27 17:12 1.49for.c
  5922. -rw-r--r--   1 cp18     cp           136 Jul 27 17:12 1.53inc.c
  5923. -rw-r--r--   1 cp18     cp           176 Jul 27 17:12 1.55symbolic.c
  5924. -rw-r--r--   1 cp18     cp           173 Jul 27 17:12 1.58copy.c
  5925. -rw-r--r--   1 cp18     cp           153 Jul 27 17:12 1.62copy2.c
  5926. -rw-r--r--   1 cp18     cp           212 Jul 27 17:12 1.64ccount.c
  5927. -rw-r--r--   1 cp18     cp           245 Jul 27 17:12 1.67lcount.c
  5928. -rw-r--r--   1 cp18     cp           322 Jul 27 17:12 1.71uccount.c
  5929. -rw-r--r--   1 cp18     cp           428 Jul 27 17:12 1.73lwccount.c
  5930. -rw-r--r--   1 cp18     cp           341 Jul 27 17:12 1.77power.c
  5931. -rw-r--r--   1 cp18     cp           393 Jul 27 17:12 1.85array.c
  5932. -rw-r--r--   1 cp18     cp           369 Jul 27 17:12 1.91square.c
  5933. -rw-r--r--   1 cp18     cp           793 Jul 27 17:12 1.94getline.c
  5934. -rw-r--r--   1 cp18     cp           675 Jul 27 17:12 1.98external.c
  5935. -rw-r--r--   1 cp18     cp           219 Jul 27 17:12 2.34conv.c
  5936. -rw-r--r--   1 cp18     cp           798 Jul 27 17:12 2.50comma.c
  5937. -rw-r--r--   1 cp18     cp           816 Jul 27 17:12 2.58itob.c
  5938. -rw-r--r--   1 cp18     cp           182 Jul 27 17:12 3.10while.c
  5939. -rw-r--r--   1 cp18     cp           163 Jul 27 17:12 3.14for.c
  5940. -rw-r--r--   1 cp18     cp           356 Jul 27 17:12 3.20wc.c
  5941. -rw-r--r--   1 cp18     cp           764 Jul 27 17:12 3.24strln.c
  5942. -rw-r--r--   1 cp18     cp           230 Jul 27 17:12 3.28bc.c
  5943. -rw-r--r--   1 cp18     cp           309 Jul 27 17:12 3.32bcg.c
  5944. -rw-r--r--   1 cp18     cp          1048 Jul 27 17:12 4.08getdbl.c
  5945. -rw-r--r--   1 cp18     cp           260 Jul 27 17:12 4.14vol_a.c
  5946. -rw-r--r--   1 cp18     cp           145 Jul 27 17:12 4.14vol_b.c
  5947. -rw-r--r--   1 cp18     cp           736 Jul 27 17:12 4.14vol_c.c
  5948. -rw-r--r--   1 cp18     cp           230 Jul 27 17:12 4.17perm.c
  5949. -rw-r--r--   1 cp18     cp           465 Jul 27 17:12 4.18block.c
  5950. -rw-r--r--   1 cp18     cp           356 Jul 27 17:12 4.26fact.c
  5951. -rw-r--r--   1 cp18     cp           355 Jul 27 17:12 4.29prepro.c
  5952. -rw-r--r--   1 cp18     cp           425 Jul 27 17:12 4.30sqrt.c
  5953. -rw-r--r--   1 cp18     cp           574 Aug  3 09:13 5.12pdemo.c
  5954. -rw-r--r--   1 cp18     cp           348 Jul 27 17:12 5.14swap_ng.c
  5955. -rw-r--r--   1 cp18     cp           403 Jul 27 17:12 5.17swap.c
  5956. -rw-r--r--   1 cp18     cp           342 Jul 27 17:12 5.20ap.c
  5957. -rw-r--r--   1 cp18     cp           186 Jul 27 17:12 5.26pcs.c
  5958. -rw-r--r--   1 cp18     cp           519 Jul 27 17:12 5.28strln_p.c
  5959. -rw-r--r--   1 cp18     cp           430 Jul 27 17:12 5.30strcpy.c
  5960. -rw-r--r--   1 cp18     cp           408 Jul 27 17:12 5.33strcpy_p.c
  5961. -rw-r--r--   1 cp18     cp           580 Jul 27 17:12 5.34strcmp.c
  5962. -rw-r--r--   1 cp18     cp           318 Jul 27 17:12 5.38mda1.c
  5963. -rw-r--r--   1 cp18     cp           359 Jul 27 17:12 5.40mda2.c
  5964. -rw-r--r--   1 cp18     cp           209 Jul 27 17:12 5.44aop.c
  5965. -rw-r--r--   1 cp18     cp           405 Jul 27 17:12 5.49argdemo.c
  5966. -rw-r--r--   1 cp18     cp           223 Jul 27 17:12 5.50echo1.c
  5967. -rw-r--r--   1 cp18     cp           221 Jul 27 17:12 5.53echo2.c
  5968. -rw-r--r--   1 cp18     cp           592 Jul 27 17:12 5.60frp.c
  5969. -rw-r--r--   1 cp18     cp           409 Jul 27 17:12 5.63pfi.c
  5970. -rw-r--r--   1 cp18     cp           367 Jul 27 17:12 6.14struct1.c
  5971. -rw-r--r--   1 cp18     cp           590 Jul 27 17:12 6.17struct2.c
  5972. -rw-r--r--   1 cp18     cp           627 Jul 27 17:12 6.20aos.c
  5973. -rw-r--r--   1 cp18     cp           775 Jul 27 17:12 6.24cbv.c
  5974. -rw-r--r--   1 cp18     cp           801 Jul 27 17:12 6.26cbr.c
  5975. -rw-r--r--   1 cp18     cp           746 Jul 27 17:12 6.34fields.c
  5976. -rw-r--r--   1 cp18     cp          1181 Aug  7 15:11 6.40unions.c
  5977. -rw-r--r--   1 cp18     cp           811 Jul 27 17:12 7.14getdbl_n.c
  5978. -rw-r--r--   1 cp18     cp           667 Jul 27 17:12 7.20cp.c
  5979. -rw-r--r--   1 cp18     cp           395 Jul 27 17:12 7.27cat.c
  5980. -rw-r--r--   1 cp18     cp           483 Jul 27 17:12 7.34sscanf.c
  5981. -rw-r--r--   1 cp18     cp           361 Jul 27 17:12 7.46atof.c
  5982. -rw-r--r--   1 cp18     cp           773 Jul 27 17:12 7.50create.c
  5983. -rw-r--r--   1 cp18     cp           769 Jul 27 17:12 7.52getinfo.c
  5984. -rw-r--r--   1 cp18     cp           414 Jul 27 17:12 7.56checknum.c
  5985. -rw-r--r--   1 cp18     cp           242 Jul 27 17:12 7.59system.c
  5986. -rw-r--r--   1 cp18     cp           788 Jul 27 17:12 7.70cp2.c
  5987. -rw-r--r--   1 cp18     cp           173 Jul 27 17:12 fact.f
  5988. -rw-r--r--   1 cp18     cp           688 Jul 27 17:12 getdbl.f
  5989. -rw-r--r--   1 cp18     cp           188 Jul 27 17:12 getdbl_a.f
  5990. -rw-r--r--   1 cp18     cp           699 Jul 27 17:12 getdbl_n.f
  5991. -rw-r--r--   1 cp18     cp           344 Jul 27 17:12 getint.f
  5992. -rw-r--r--   1 cp18     cp           400 Jul 27 17:12 getintp.f
  5993. -rw-r--r--   1 cp18     cp           391 Jul 27 17:12 getline.f
  5994. -rw-r--r--   1 cp18     cp           387 Aug  3 14:21 getlinep.f
  5995. -rw-r--r--   1 cp18     cp           439 Jul 27 17:12 getlinepn.f
  5996. -rw-r--r--   1 cp18     cp           177 Jul 27 17:12 inter.f
  5997. -rw-r--r--   1 cp18     cp           285 Jul 27 17:12 itob.f
  5998. -rw-r--r--   1 cp18     cp           163 Jul 27 17:12 power.f
  5999. -rw-r--r--   1 cp18     cp           155 Jul 27 17:12 square.f
  6000. -rw-r--r--   1 cp18     cp           204 Jul 27 17:12 strcmp.f
  6001. -rw-r--r--   1 cp18     cp           113 Jul 27 17:12 strcpy.f
  6002. -rw-r--r--   1 cp18     cp            85 Jul 27 17:12 strcpy_p.f
  6003. -rw-r--r--   1 cp18     cp           204 Jul 27 17:12 strln.f
  6004. -rw-r--r--   1 cp18     cp           228 Jul 27 17:12 strln_p.f
  6005. -rw-r--r--   1 cp18     cp           140 Jul 27 17:12 swap.f
  6006. -rw-r--r--   1 cp18     cp           125 Jul 27 17:12 vol.f
  6007. $
  6008. w-r--r--   1 cp18     cp           140 Jul 27 17:12 swap.f
  6009. -rw-r--r--   1 cp18     cp           125 Jul 27 17:12 vol